unity3d 四元数,Lerp函数给出误差;表达式Assert失败:'!比较近似值(标量,0.0F)'

cetgtptt  于 2022-12-30  发布在  其他
关注(0)|答案(1)|浏览(225)

我正在尝试制作一个系统,用户可以使用空格键逐渐将车辆转向90度,并且在按住空格键时车辆保持完全转向,但在输入停止时车辆逐渐返回0度旋转。
当我把一个逐渐上升和下降的定时器变量放入Lerp函数时,它只会给出错误:表达式Assert失败:'!比较近似值(标量,0.0F)'

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Movement : MonoBehaviour
{
    private static float turnTimer = 2f;
    private float turnTimeElapsed;
    private float clampedTimeElapsed;
    private float lerpTimer;

    [SerializeField] private float turnSpeed;

    Quaternion endRot;

    Rigidbody ridgy;

    private void Start()
    {
        ridgy = this.GetComponent<Rigidbody>();

        turnTimeElapsed = 0f;

        Quaternion endRot = Quaternion.Euler(0f, 90f, 0f);
    }

    private void Update()
    {
        if (Input.GetKey(KeyCode.Space) && turnTimeElapsed < turnTimer)
        {
            turnTimeElapsed += turnSpeed * Time.deltaTime;
            
        }
        else if (!Input.GetKey(KeyCode.Space) && turnTimeElapsed > 0)
        {
            turnTimeElapsed -= turnSpeed * Time.deltaTime;
        }

        clampedTimeElapsed = Mathf.Clamp(turnTimeElapsed, 0f, turnTimer);

        lerpTimer = clampedTimeElapsed / turnTimer;

        transform.rotation = Quaternion.Lerp(Quaternion.identity, endRot, lerpTimer);
    }
}
nnt7mjpx

nnt7mjpx1#

在这里,您实际上不需要使用lerp,直接构造quat即可。
90 degrees--〉90 * PI/180 * 0.5--〉PI/4 [四元数取半角,因此取0.5]
鉴于季铵被定义为:

xyz = sin(angle/2) * Axis
w = cos(angle/2)

你想绕y轴旋转(0,1,0),所以直接构造四元数。

transform.rotation = new Quaternion(
 0,
 Mathf.Sin(lerpTimer * Mathf.PI/4.0),
 0,
 Mathf.Cos(lerpTimer * Mathf.PI/4.0)
);

相关问题