- Home /
Question by
angrycarrots · Feb 14, 2013 at 06:33 AM ·
c#rotatelimit
Im new, please help me with rotation! Thanks
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!!
Comment
Best Answer
Answer by OmarAlhaddad · Feb 14, 2013 at 01:11 PM
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
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
C# Rotate More than Two GameObjects 1 Answer