I am working on a Unity project. There is a car wiper and i want to rotate it in a way that when I pres "wipers toggle on" wipers start rotating from 0,0,0 to 0,0,-45 and start lerping. But when I press "wipers toggle off" the wipers must rotate back to 0,0,0. For example if current wipers rotation are 0,0,-20 and I press the "wipers toggle off" key they wipers must be rotateTowards 0,0,0. Also, If I press "wipers toggle on" again the wipers must start rotating from 0,0,0 to 0,0,-45. Now, the situation is that Wipers are rotating but when I press "wipers toggle off" the wipers stop at exactly the same current rotation point where they are for example (0,0,-30). And when I press "wipers toggle on" again the wipers starts from weird different rotation point. Here is my code:
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);
}
}
}