【问题标题】:How to check rotation of an object in Unity3d?如何在 Unity3d 中检查对象的旋转?
【发布时间】:2014-12-18 23:42:46
【问题描述】:

我是一名 unity3d 学习者。我有一个物体旋转的问题。我想将对象沿 z 轴旋转约 40 度。如果对象旋转达到 40 度,我希望发生一些事情。这是我的代码。

foreach(Touch touch in Input.touches) {
    if(touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled) {
        var target = Quaternion.Euler (0, 0,-40);
        transform.rotation = Quaternion.Slerp(transform.rotation, target, Time.deltaTime * smooth);
        if (transform.rotation.eulerAngles.z == -40) {
            toggle = true;
            speech = "blah blah blah";
            snake = man;
        } 
    }   
}

if(transform.rotation.eulerAngles.z == -40) 代码行不起作用。所以我不知道旋转度数是否达到了40度。如何查看旋转度数是否达到40度?

【问题讨论】:

  • 如果它是浮点数或双精度数,则不应使用 == 来检查是否相等,因为该值可能仅相差一小部分。试试 abs(z-(-40))
  • 我使用度数。但是绝对功能是行不通的。谢谢。

标签: unity3d rotation


【解决方案1】:

我不明白你的代码意图。

EulerAngles 没有设置为负值,只有正值(Vector3)
-if(eulerAngles.z == -40) 不起作用尝试更改值 -40 -> 320
如果你想要场景
ontouch -> 对象旋转 -> 事件
试试这个代码。

float rotTime = 1f; // rotation duration
Vector3 rotValue = new Vector3(0, 0, -40f); // rotation value

void Update () {
    foreach (Touch touch in Input.touches)
        if (touch.phase == TouchPhase.Began) OnTouchEvent();
}

void OnTouchEvent()
{
    StopCoroutine("rotationCoroutine"); 
    StartCoroutine("rotationCoroutine");
}
IEnumerator rotationCoroutine()
{
    float startTime = Time.time;

    Vector3 startRot = transform.eulerAngles;
    Vector3 endRot = startRot;
    endRot += rotValue;

    while (Time.time - startTime <= rotTime)
    {
        transform.eulerAngles = Vector3.Slerp(startRot, endRot,(Time.time - startTime) / rotTime);
        yield return null; // wait 1 frame
    }
    //rotation end
    MyAction();
}

void MyAction()
{
    Debug.Log("rotation end");
    //toggle = true;
    //speech = "blah blah";
    //snake = man;
}

祝你好运:D

【讨论】:

    猜你喜欢
    • 2019-08-09
    • 1970-01-01
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2019-07-15
    • 1970-01-01
    • 1970-01-01
    • 2016-07-29
    相关资源
    最近更新 更多