Last active
November 14, 2019 01:52
-
-
Save takashi1975/b5edd93bea2bd6a5f620c9ce67358bc2 to your computer and use it in GitHub Desktop.
Quaternion.LookRotation 例
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
//目標の方を向く例 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); | |
} | |
} |
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
//目標の方を向く例 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); | |
} | |
} |
Author
takashi1975
commented
Nov 14, 2019
•
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment