- Home /
How to reset a game object to its original rotation.
I have a game object that when clicked, moves upwards a little, and rotates to demonstrate selection. I am trying to get it so that when I click the object again (deselect it), the object moves back down to the original way it was facing.
Moving the object down again is no problem, however I am having trouble getting the object to rotate, so that if faces the way it was originally facing. This is the latest code I have tried, however when I deselect the object it simply moves back down and keeps its new rotation.
public Transform originalRotationValue;
float rotationResetSpeed = 1.0f;
void Start () {
originalRotationValue = gameObject.transform;
}
void Update () {
//rotate selected piece
if(isSelected)
gameObject.transform.Rotate(0, 1, 0);
//otherwise rotate it back to its original rotation
else
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue.rotation, Time.time * rotationResetSpeed);
}
What am I missing/doing wrong?
You should try storing the rotation of the Transform at the Start, not the transform. The transform you have defined will change with the code so the start value
Answer by aldonaletto · Oct 13, 2012 at 09:51 PM
You're saving the object's transform, but should instead save the original rotation!
public Quaternion originalRotationValue; // declare this as a Quaternion
float rotationResetSpeed = 1.0f;
void Start () {
originalRotationValue = transform.rotation; // save the initial rotation
}
void Update () {
//rotate selected piece
if(isSelected)
gameObject.transform.Rotate(0, 1, 0); // this rotates only 1 degree!
//otherwise rotate it back to its original rotation
else
transform.rotation = Quaternion.Slerp(transform.rotation, originalRotationValue, Time.time * rotationResetSpeed);
}
2020 still works Has anyone told you yet that you are a life saver