簡體   English   中英

為什么在 z 軸上旋轉對象時,它也會旋轉 x 和 y 而沒有將 z 設置為 0?

[英]Why when rotating the object on z axis it's rotating also the x and y and not setting z to 0?

我希望對象僅在 z 上旋轉。 保持 x 和 y 並將 z 更改為 0。

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Whilefun.FPEKit;

public class PlayAnimation : MonoBehaviour
{
    public List<GameObject> cameras = new List<GameObject>();
    public GameObject head;

    private Animator anim;
    private bool started = true;
    private float animationLenth;
    private bool rotateHead = false;

    // Start is called before the first frame update
    void Start()
    {
        anim = GetComponent<Animator>();
    }

    private void Update()
    {
        if (FPESaveLoadManager.gameStarted == true && started == true)
        {
            FPEInteractionManagerScript.Instance.BeginCutscene();

            anim.enabled = true;
            anim.Play("Stand Up", 0, 0);
            animationLenth = anim.GetCurrentAnimatorStateInfo(0).length;
            StartCoroutine(AnimationEnded());
            started = false;
        }

        if(rotateHead == true)
        {
            head.transform.rotation = Quaternion.Lerp(transform.rotation, Quaternion.Euler(transform.rotation.x, transform.rotation.y, 0f), 1.0f * Time.deltaTime);
        }
    }

    IEnumerator AnimationEnded()
    {
        yield return new WaitForSeconds(animationLenth);

        anim.enabled = false;
        FPEInteractionManagerScript.Instance.EndCutscene();
        rotateHead = true;
    }
}

我正在嘗試旋轉頭部。 動畫結束時頭部旋轉為:X = -9.922001 Y = 6.804 Z = 4.167

當頭部的旋轉結束時,或者因為它在更新中它永遠不會結束? 頭部旋轉是: X = -4.529 Y = -9.392 Z = 0.988 為什么 x 和 y 改變而 z 永遠不會達到 0 ?

我現在看到的另一個奇怪的事情是動畫結束時 X 上的變換位置是 -5.089218e-12 這個 e-12 是什么?

我在 Musaps 上所說的一個例子:

using UnityEngine;

public class ExampleLerp: MonoBehaviour
{
    public float Start = 2.0f;
    public float End = 0.0f;
    public float TimeToTravel = 3.0f;

    private float elapsedTime = 0.0f;
    private float inverseTime;
    // Start is called before the first frame update
    void Start()
    {
      inverseTime = 1.0f/TimeToTravel;
    }

    private void Update()
    {
      float ratio = 0.0f;   
      // Increment our elapsedTime
      elapsedTime += Time.deltaTime;
      if(elapsedTime > TimeToTravel)
      {
        ratio = 1.0f;
      }
      else
      {
        ratio = elapsedTime * inverseTime // Or can be done as elapsedTime/TimeToTravel
      }

      Debug.log("value is: " + Math.Lerp(Start, End, ratio));

    }
}

通過使用當前的旋轉,並使用相同的目標旋轉,而不是隨着時間的推移存儲你的進度,你會在接近終點時看到減速,但它永遠不會在數學上真正到達終點。

首先,e-12 是 10^-12 或者在你的情況下,它是 -0.000000000005089218。 發生這種情況可能是因為浮點數學。

z 不會達到 0,因為您正在使用 lerp 函數而沒有在途中改變插值比率。 Lerp 代表線性插值。 它獲得的第三個參數(代碼中的1.0f * Time.deltaTime )是插值比。 如果該比率為 0.5,則 z 的值將跳至差值的中間值(即 (0 - 4.167) * 0.5)。 例如; Lerp(0, 1, 0.5f) 使它在下一幀跳到 0.5,在另一幀跳到 0.75。

讓我們考慮一下您的游戲以每秒 60 幀的速度運行。 這使得 Time.deltaTime = 1/60。 當 Lerp 在您的代碼中開始執行時,z 在第一幀中為 4.167。 你希望它達到零。 插值的比率是 1.0f * Time.deltaTime,即 0.01666... 或 1/60。 這意味着 z 將跳到差異的 1/60。

在第二幀中,z 達到 (4.167 + (0 - 4.167) / 60) = 4.09755 在第三幀中,z 達到 (4.09755 + (0 - 4.09755 / 60) = 4.0292575

您可以通過根據容差值測試 z 值來克服這個問題。

if (rotateHead)
{
    const float end = 0f;
    var rotation = transform.rotation;
    var z = Mathf.Lerp(rotation.z, end, 1.0f * Time.deltaTime);
    const float tolerance = 1f;
    if (Math.Abs(z - end) < tolerance) z = 0;
    head.transform.rotation = Quaternion.Euler(rotation.x, rotation.y, z);
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM