【发布时间】:2016-12-24 21:01:57
【问题描述】:
我创建了这段代码,但它不起作用,我不明白为什么。在 Debug.Log 中,一切似乎都很好,但 while 循环永远是正确的。有没有更好的方法将游戏对象左右旋转 90 度?
https://www.youtube.com/watch?v=7skGYYDrVQY
//Public
public float rotateSpeed = 150f;
public bool isMoving = false;
//Private
private Rigidbody myRigidbody;
void Start () {
myRigidbody = GetComponent<Rigidbody> ();
}
void Update() {
Quaternion originalRotation = this.transform.rotation;
if (!isMoving) {
if (Input.GetButtonDown ("PlayerRotateRight")) {
//Rotate Right
StartCoroutine (PlayerRotateRight (originalRotation));
} else if (Input.GetButtonDown ("PlayerRotateLeft")) {
//Rotate Left
StartCoroutine (PlayerRotateLeft (originalRotation));
}
}
}
IEnumerator PlayerRotateRight(Quaternion originalRotation) {
Quaternion targetRotation = originalRotation;
targetRotation *= Quaternion.AngleAxis (90, Vector3.up);
while (this.transform.rotation.y != targetRotation.y) {
Debug.Log ("Current: " + this.transform.rotation.y);
Debug.Log ("Target: " + targetRotation.y);
isMoving = true;
transform.rotation = Quaternion.RotateTowards (transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
yield return null;
}
isMoving = false;
}
IEnumerator PlayerRotateLeft(Quaternion originalRotation) {
Quaternion targetRotation = originalRotation;
targetRotation *= Quaternion.AngleAxis (90, Vector3.down);
while (this.transform.rotation.y != targetRotation.y) {
Debug.Log ("Current: " + this.transform.rotation.y);
Debug.Log ("Target: " + targetRotation.y);
isMoving = true;
transform.rotation = Quaternion.RotateTowards (transform.rotation, targetRotation, rotateSpeed * Time.deltaTime);
yield return null;
}
isMoving = false;
}
【问题讨论】:
标签: c# unity3d rotation transform