Last active
February 22, 2024 02:50
-
-
Save Nobuyuki-Kobayashi/d83ad6f9e029745099e7e62103094be2 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// https://qiita.com/Akematty/items/04fadac40f7ea45fd5ab | |
// https://github.com/Unity-Technologies/UnityCsReference/blob/master/Runtime/Animation/ScriptBindings/LookAtConstraint.bindings.cs | |
// を参考に、LookAt Constraint版ペアレントを動的に追加するヘルパー | |
// | |
using UnityEngine; | |
using UnityEngine.Animations; | |
namespace UnityChan | |
{ | |
public class LookAtConstraintHelper : MonoBehaviour { | |
//Warning CS0649が出る場合の対策. | |
//https://qiita.com/nonkapibara/items/8cd26751e3c86451a563 | |
#pragma warning disable 0649 | |
// | |
private ConstraintSource myConstraintSource; | |
private LookAtConstraint myLookAtConstraint; | |
[SerializeField] | |
public Transform SourceTransform; | |
// LookAtConstraint コンポーネントの親オブジェクトを動的に追加する | |
private void SetObject2ConstraintSource(Transform parent) { | |
// Constraintの参照元を設定(この処理が一つでも欠けると追尾せず即死する) | |
this.myConstraintSource.sourceTransform = parent; | |
this.myConstraintSource.weight = 1.0f; // 影響度を完全支配にする(Addの場合は0になるので) | |
this.myLookAtConstraint.AddSource(this.myConstraintSource); | |
this.myLookAtConstraint.useUpObject = false; | |
this.myLookAtConstraint.roll = 0.0f; | |
this.myLookAtConstraint.worldUpObject = null; | |
this.myLookAtConstraint.locked = true; | |
this.myLookAtConstraint.rotationAtRest = Vector3.zero; | |
this.myLookAtConstraint.rotationOffset = Vector3.zero; | |
} | |
// Constraintの設定を解除する | |
private void DeleteSource() { | |
this.myLookAtConstraint.RemoveSource(0);// Sourceから削除 | |
this.myLookAtConstraint.enabled = false;// 無効にする | |
} | |
private void Awake() { | |
this.myLookAtConstraint = GetComponent<LookAtConstraint>(); | |
if(this.SourceTransform == null){ | |
DeleteSource(); | |
}else{ | |
SetObject2ConstraintSource(this.SourceTransform); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment