Hey everyone! I’m trying to rotate a 2d object on the Z axis when you left click, and rotate it back to it’s start position when you release left mouse. I’m using Quaternion.AngleAxis to lerp the rotation, and I have a series of variables set up for each one.
public bool rotateIt = false;
private float rotateTimer = 0f;
private float rotatePercentage = 0f;
public bool unRotateIt = false;
private float unRotateTimer = 0f;
private float unRotatePercentage = 0f;
The code below is in the update:
if (Input.GetKeyDown(KeyCode.Mouse0))
{
isClicking = true;
waterParticles.GetComponent<ParticleSystem>().Play();
rotateIt = true;
unRotateIt = false;
rotateTimer = 0f;
}
if (Input.GetKeyUp(KeyCode.Mouse0))
{
isClicking = false;
rotateIt = false;
if (transform.rotation.z >= 0f)
{
unRotateIt = true;
unRotateTimer = 0f;
}
}
if (rotateIt == true)
{
float startRotation = transform.rotation.z;
float endRotation = 20f;
rotateTimer += 40f * Time.deltaTime;
float rotatePercentage = Mathf.Clamp(rotateTimer, 0f, 20f) / 20f;
transform.rotation = Quaternion.AngleAxis(Mathf.Lerp(startRotation, endRotation, rotatePercentage), Vector3.forward);
}
if (unRotateIt == true)
{
float startUnRotation = transform.rotation.z;
float unEndRotation = 0f;
unRotateTimer += 40f * Time.deltaTime;
float unRotatePercentage = Mathf.Clamp(unRotateTimer, 0f, 20f) / 20f;
Debug.Log("unrotatepercentage: " + unRotatePercentage);
transform.rotation = Quaternion.AngleAxis(Mathf.Lerp(startUnRotation, unEndRotation, unRotatePercentage), Vector3.forward);
}
My expectation / hope is that this would cause the object to rotate over time in one direction when you click, and start rotating back the other direction when you release. This isn’t doing quite that though- when you hold left mouse the obj rotates in the first direction like expected, but when left mouse is released it instantly goes back to the start position instead of lerping. I’ve tried quite a few fixes for this but I can’t tell what’s causing it. Anyone know how to solve this issue? Much appreciated!