【问题标题】:Unity track object rotationunity track 物体旋转
【发布时间】:2014-10-14 08:21:53
【问题描述】:

我有几个对象在FixedUpdate() 中自行旋转。
现在我需要跟踪一个对象的旋转,我们称之为objX。当我检索它时,旋转仅从 0 到 360。 360度后可以旋转吗?
例如当我使用类似的东西时

float x = objX.transform.rotation.z;

变量 x 应该是 560 度。
这样的事情可能吗?

【问题讨论】:

  • 添加一个计数器来计算翻转并增加它 + 当前的 rotation.z ?
  • 我不能这样做,因为我用鼠标旋转它,当我拉鼠标太快时它会跳过我做增量的点。
  • 哦,我明白了,也许是THIS
  • 不工作。仍然从 360 到 0。

标签: unity3d rotation angle gameobject


【解决方案1】:

这是跟踪旋转的方法。

Vector3 mouseClickPos;
    float angle;
    float lastAngle;
    int fullRotations;
    public float realAngle;

    void FixedUpdate(){
        mouseClickPos = Input.mousePosition;    
        Vector3 dir = mouseClickPos - Camera.main.WorldToScreenPoint(transform.position);
        angle = Mathf.Atan2(dir.y,dir.x) * Mathf.Rad2Deg;
        angle-=90;
        if(lastAngle - angle > 270){
            fullRotations ++;
        }else if(angle - lastAngle > 270){
            fullRotations --;
        }
        Debug.Log(360*fullRotations + angle);
        Quaternion q = Quaternion.AngleAxis(angle, Vector3.forward);
        transform.rotation = Quaternion.RotateTowards(transform.rotation, q, 6);
        lastAngle = angle;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-13
    相关资源
    最近更新 更多