- Home /
How to return gameobjects rotation to original Rotation / zero rotation?
,I've managed to make the object rotate like I want, except that it doesn't go back to it's original rotation when I let go off the buttons. I'm using Input.GetAxis incase I wanna try it out with a gamepad, I couldn't find any solution for it while using that. I saw some solutions when using normal Input.GetKey.
I don't know if you need to see the code but here it is:
rwfRotation -= Input.GetAxis("Vertical");
rwfRotation = Mathf.Clamp(rwfRotation, -2, 4);
RightWingFlap.transform.localEulerAngles = new Vector3(rwfRotation * 5, RightWingFlap.transform.localEulerAngles.y, RightWingFlap.transform.localEulerAngles.z);
This makes the flaps turn up and/or down when I press either S or W. I want the flap to return to it's original rotation when I'm not pressing S or W. The variable name might be a bit weird, but trying to make a airplane, with flaps :)
Store the original rotation of the game object, and when you are not giving any input, return to that value.
A more sophisticated approach is to encapsulate this into a component, that stores its original local rotation in its Start()
method, and ins$$anonymous$$d of directly controlling its rotation, you create functions that tell it that you want it to increase or decrease rotation, and store that value as desiredRotation = currentRotation + change
. Then in Update()
you limit the desiredRotation
(like $$anonymous$$athf.Clamp()
, and use Quaternion.Lerp()
to go towards the desiredRotation
, so it will achieve it over time. Finally, at the end of Update()
you set desiredRotation
to originalRotation
, so it will always want to return to it when there is no input.
Answer by tormentoarmagedoom · Jun 06, 2018 at 03:26 PM
Good day.
You just need to store the original position and rotation. Does not care how you move/rotate it, or what inputs are you detecting.
If at the begining of your script, at the Start, you store the position and rotation like this:
Vector3 InitialPosition;
Quaternion InitialRotation;
void Start()
{
InitialPosition = gameObject.transform.position;
InitialRotation = gameObject.transform.rotation;
}
Then, simply when you want it to come back (maybe when some Input is null or i dont know) you need to change its position an rotation again
gameObject.transform.position = InitialPosition;
gameObject.transform.rotation = InitialRotation;
Bye!
I think I solved it, I had to move it to update, since It's a part on my plane that I'm rotating, when I had it in start it always wanted to rotate back to like start value, not caring which direction the plane is in. But moving it to update seemed to solve it, now I just need to figure out how to make it when there is no input. Anyone got an idea about that? How to recognize no input for Input.GetAxis.
Check out my comment: have desiredRotation
, and reset its value to originalRotation
at the end of Update()
.
Yea I saw your comment, I just didn't really understand, sorry. I'm not that experienced.
if (Input.GetAxis("Yaw") >= -0.1f && Input.GetAxis("Yaw") <= 0.1f) { Rudder.transform.rotation = OriginalRotation; }
kinda works... problem is that, it stores the old rotation value, so that when I give a input after it has returned to 0, it jumps back to the old rotation and then starts from there, and not from 0