Hi, please help me. (I use c# by the way). I want something to rotate when i press “e” but i want it to stop rotating after reaching a certain number. Then when I release “e”, i want it to rotate back. Thanks and please explain the code in c# thanks so much!!
You can use Vector3.Lerp to interpolate between two values.
This following code will rotate the Z of your game object 180 degrees as per the behaviour you described:
float pct = 0
float speed = 0.4f
void Update()
{
if (Input.GetKey(KeyCode.Mouse0))
{
if (pct < 1) {
pct += Time.deltaTime * speed;
transform.rotation = Quaternion.Euler (Vector3.Lerp(new Vector3(0,0,0), new Vector3(0,0,180f), pct ));
print(pct);
}
} else {
if (pct > 0) {
pct -= Time.deltaTime * speed;
transform.rotation = Quaternion.Euler (Vector3.Lerp(new Vector3(0,0,0), new Vector3(0,0,180f), pct ));
print(pct);
}
}
}
Goodluck