繁体   English   中英

旋转Object平滑90度

[英]Rotate Object Smooth 90 degrees

天呐早安我想在 if 语句中将 object 平滑旋转 90 度。 我到处寻找解决方案,但找不到适合我需要的解决方案:这就是我想要的样子:

if (Swipe.Left)
{ 
    Object smooth rotate 90 degrees down //left
}

我希望有人知道我该怎么做:感谢您的帮助 :)

编辑:我以前试过这个,但它似乎不适用于 if 语句:

Quaternion newRotation = Quaternion.AngleAxis(90, Vector3.down);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, .03f);

由于您的Swipe.Left仅在一帧内返回 true,因此您必须将逻辑维持更长的时间。 为此,让我们在滑动时启用一个标志,并在达到目标旋转时禁用该标志。

if (Swipe.Left)
{ 
     swiped = true;
     newRotation = Quaternion.AngleAxis(90, Vector3.down);
     slerpEase = .03f;
}

if (swiped)
{ 
    transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, slerpEase);
    slerpEase *= 0.95f;
    if (Mathf.Approximately(1f, Quaternion.Dot(transform.rotation, newRotation)))
        swiped = false; // reached the target rotation
}

另请注意,当 object 随着时间的推移旋转时,我使用了一个名为slerpEase的变量来平滑地减慢旋转速度。 您可能想要更改0.95f以使其依赖于增量时间。

请注意,如果您围绕两个轴旋转它,由于万向节锁定,比较角度会有点棘手。

我会为此使用协程,并使用AnimationCurve来确定转弯的平滑度。 这将使您可以仅使用检查器微调您想要的外观,并且如果您想要不同的程度,它可以很好地重用代码。

[SerializeField] private AnimationCurve swipeRotateAnimationCurve; // configure in inspector
private Coroutine swipeRotateCoroutine = null;
private float swipeRotationDuration = 2f; // duration of rotation in seconds

// ...

if (Swipe.Left)
{ 
    // Cancel any currently running coroutine
    if (swipeRotateCoroutine != null) 
    {
        StopCoroutine(swipeRotateCoroutine);
        swipeRotateCoroutine = null;
    }

    swipeRotateCoroutine = StartCoroutine(DoHorizontalSwipeRotate(-90f));
}
else if (Swipe.Right)
{ 
    // Cancel any currently running coroutine
    if (swipeRotateCoroutine != null) 
    {
        StopCoroutine(swipeRotateCoroutine);
        swipeRotateCoroutine = null;
    }

    swipeRotateCoroutine = StartCoroutine(DoHorizontalSwipeRotate(90f));  
}

// ...

private IEnumerator DoHorizontalSwipeRotate(float degreesRight)
{
    float t = 0;
    Quaternion startRot = transform.rotation;

    // update rotation until 
    while (t < 1f)
    {
        // let next frame occur
        yield return null;

        // update timer
        t = Mathf.Min(1f, t + Time.deltaTime/swipeRotationDuration); 
       
        // Find how much rotation corresponds to time at t:
        float degrees = degreesRight * swipeRotateAnimationCurve.Evaluate(t);

        // Apply that amount of rotation to the starting rotation:
        transform.rotation = startRot * Quaternion.Euler(0f, degrees, 0f);
    }

    // allow for next swipe
    swipeRotateCoroutine = null;
}

因此,当涉及到 Unity 中的方向时,它并不总是我们认为应该的那样我已经在这里回答了与此相关的问题。 因此,在您顺时针旋转 object 的情况下,您需要使用Vector3.forward而不是Vector3.down 这包括围绕您的 object 将要旋转的女巫定义一个轴。

现在让我们谈谈像rotate to right rotate to leftclockwise / counter Clockwise这样的方向。 当你想顺时针旋转你的 object 时,你使用 -ve(想象)角度,当你想要逆时针旋转你的 object 时,你使用 +ve(正)角度。

所以你的代码应该如下顺时针旋转你的 object :

Quaternion newRotation = Quaternion.AngleAxis(90, Vector3.forward);
transform.rotation = Quaternion.Slerp(transform.rotation, newRotation, 0.03f);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM