Skip to content

Instantly share code, notes, and snippets.

@takashi1975
Last active November 14, 2019 01:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takashi1975/b5edd93bea2bd6a5f620c9ce67358bc2 to your computer and use it in GitHub Desktop.
Save takashi1975/b5edd93bea2bd6a5f620c9ce67358bc2 to your computer and use it in GitHub Desktop.
Quaternion.LookRotation 例
//目標の方を向く例 1
public class Aim : MonoBehaviour
{
[SerializeField]
private GameObject _target;
void Update()
{
var directionToFace = this._target.transform.position - this.transform.position;
//y固定 にしたい場合
directionToFace.y = 0f;
this.transform.rotation = Quaternion.LookRotation(directionToFace);
Debug.DrawRay(this.transform.position, directionToFace, Color.green);
}
}
//目標の方を向く例 2
public class Aim : MonoBehaviour
{
[SerializeField]
private GameObject _target;
[SerializeField]
private float _speed = 5f;
void Update()
{
var directionToFace = this._target.transform.position - this.transform.position;
//y固定 にしたい場合
//directionToFace.y = 0f;
var targetRotation = Quaternion.LookRotation(directionToFace);
this.transform.rotation = Quaternion.Slerp(this.transform.rotation, targetRotation, Time.deltaTime * this._speed);
Debug.DrawRay(this.transform.position, directionToFace, Color.green);
}
}
@takashi1975
Copy link
Author

takashi1975 commented Nov 14, 2019

Debug.DrawRay(this.transform.position, directionToFace, Color.green);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment