我正在开发一个 Unity 项目。有一个汽车雨刷器,我想旋转它,当我按下“雨刷器开启”时,雨刷器开始从 0,0,0 旋转到 0,0,-45 并开始转动。但是当我按下“雨刷关闭”时,雨刷必须旋转回 0,0,0。例如,如果当前雨刷旋转为 0,0,-20,并且我按下“雨刷切换关闭”键,则雨刷必须向 0,0,0 旋转。此外,如果我再次按下“wipers toggle on”,则雨刷必须开始从 0,0,0 旋转到 0,0,-45。现在,情况是雨刷器正在旋转,但是当我按下“雨刷器关闭”时,雨刷器停止在完全相同的当前旋转点,例如(0,0,-30)。当我再次按下“雨刷器开启”时,雨刷器从奇怪的不同旋转点开始。这是我的代码:
using UnityEngine;
using System.Collections;
public class Wipers : MonoBehaviour
{
[SerializeField] protected Vector3 m_from = new Vector3(0.0F, 0.0F, 0.0F);
[SerializeField] protected Vector3 m_to = new Vector3(0.0F, -45.0F, 0.0F);
[SerializeField] protected float m_frequency = 1.0F;
protected virtual void Update()
{
if (ControlFreak2.CF2Input.GetAxis ("wipers") != 0)
{
Quaternion from = Quaternion.Euler(this.m_from);
Quaternion to = Quaternion.Euler(this.m_to);
float lerp = 0.5F * (1.0F + Mathf.Sin(Mathf.PI * Time.realtimeSinceStartup * this.m_frequency));
this.transform.localRotation = Quaternion.Lerp(from, to, lerp);
}
}
}