【问题标题】:How to Rotate on Y Axis by 90 degrees left or right?如何在 Y 轴上向左或向右旋转 90 度?
【发布时间】: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


    【解决方案1】:
        if (Input.GetKeyDown (KeyCode.A)) {
            transform.Rotate (0, -90, 0, Space.World);
    
    
        }else if(Input.GetKeyDown(KeyCode.D)){
            transform.Rotate(0,90,0, Space.World);
        }
    

    根据您的说法,像这样简单的事情应该可以正常工作。你选择做这么复杂的任何特殊原因?在这种情况下,我可以修改答案以适合您的代码。

    【讨论】:

    • 使用这种简单的方式它会立即旋转我想像视频中显示的那样缓慢旋转,而且我想在使用 isMoving = true 缓慢旋转的同时禁用任何其他运动;
    【解决方案2】:

    您正在比较四元数而不是欧拉角。而且最好有一个阈值,因为您要比较两个floats。

    改变一下

    while (this.transform.rotation.y != targetRotation.y)

    while (this.transform.eulerAngles.y != targetRotation.eulerAngles.y)

    while (Mathf.Abs(this.transform.eulerAngles.y - targetRotation.eulerAngles.y) &gt; THRESHOLD)

    其中THRESHOLD 是一个足够小的常数。还要在循环之后将旋转设置为精确的角度,以防止旋转错误。

    【讨论】:

    • 它可以工作,但它们不是完美的 90 度旋转,如果你旋转 50 次,你最终会变成 5 度而不是 0。我正在尝试找到一种方法来将旋转设置为完美的 90现在在外面度数。
    • 非常感谢,这几天我一直在努力寻找这个。第二种方法效果很好,我不需要使用这种方法将旋转设置为精确的角度。
    猜你喜欢
    • 2020-03-27
    • 2023-04-10
    • 1970-01-01
    • 2018-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-05
    相关资源
    最近更新 更多