- Home /
Rotation to certain degrees while holding a button? C#
On my 3rd person shooter, when I press shift, it makes the character sprint. Since I am still quite a novice at working around game scripting, I need help with a certain feature I wish to add.
When the shift button is held, I want the character's gun to rotate upwards. The problem is, I only know how to script something to keep rotating as long as the button is pressed.
I plan to make this an independent script, so I have no reference. Can anyone point out how I can rotate an object to a certain amount of degrees while a button is pressed, and when the button is released, the object will rotate to it's original position?
If anyone can help me out, thank you so much!
Answer by PProductions · Sep 08, 2012 at 07:42 PM
I belive using the Vector3.Lerp function would work for this. I have not tested this but the code would run something like this.
public float speed = 1;
public Vector3 startRotation;
public Vector3 endRotation;
void Update() {
if(Input.GetButton(KeyCode.LeftShift)) {
transform.rotation.eulerAngles = Vector3.Lerp(startRotation, endRotation, speed * Time.deltaTime);
}
else {
transform.rotation.eulerAngles = Vector3.Lerp(endRotation, startRotation, speed * Time.deltaTime);
}
}
Set the start and end Vector3 to the appropriate rotations in degrees. Hope this works!