- Home /
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 :)
Answer by robertbu · Mar 09, 2014 at 04:15 PM
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613135815im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Problem with getkey/getkeydown 1 Answer
Different views for graph plotting 0 Answers
Real time graphing 0 Answers
Draw the GL.Vertex3 at specific position 1 Answer
How to make a smooth zoom camera script with maximums? 1 Answer