簡體   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