go back to normal?

is there a way to make a game object (has gravity, is player controlled) to go back to a certain angle (y axis) automatically when it collides with something (other game object) so it doesn’t fall over (it can fall over but it comes back to idle state) ? if so, can you send me a copy of a script (C#) that can fix my issue?
thank you very much :slight_smile:

Unity Answers is to help you code your own answers, not write or provide scripts for you. And the answer will likely depend on the nature of your behavior. I’ll give you a starting point, but you’ll likely have to play and expand on it to get something that works the way you want in context.

Here are a couple of lines of code that work work to bring your object back upright. To start with just put them in FixedUpdate() and run them all the time. Later you may want to only execute them in specific situations. To start with a low value for ‘speed’:

Quaternion qTo = Quaternion.FromToRotation(transform.up, Vector3.up) * transform.rotation;
transform.rotation = Quaternion.RotateTowards(transform.rotation, qTo, Time.deltaTime * speed);

If you want an eased movement, replace the ‘RotateTowards’ with ‘Slerp’ and adjust ‘speed’ downwards.