I have an object(shark) moving up and down and when it's at the bottom, it rotates 180 degrees. But sometimes it turns over it's belly and sometimes over it's back. I want to have it so that it always rotates on it's belly and not on it's back. How can I fix this?
Also the starting rotation is -90 degrees X axis. Here's the code:
transform.Translate(Vector3.forward * speed * Time.deltaTime);
targetAnglesUp = transform.eulerAngles + 180 * Vector3.right; // what the new angles should be
targetAnglesDown = transform.eulerAngles + 180 * Vector3.right; // what the new angles should be
if (transform.position.y >= 150)
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAnglesUp, smooth * Time.deltaTime); // lerp to new angles
if (transform.position.y <= 50)
transform.eulerAngles = Vector3.Lerp(transform.eulerAngles, targetAnglesDown, smooth * Time.deltaTime); // lerp to new angles
Answer by BilboBaggins27
Mar 26 at 05:53 PM
You can add a Riggidbody (2D or 3D) and then scroll down to Constraints, and freeze its rotation so that it can only rotate on certain axis'.
Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.
The best place to ask and answer questions about development with Unity.
To help users navigate the site we have posted a site navigation guide.
If you are a new user to Unity Answers, check out our FAQ for more information.
Make sure to check out our Knowledge Base for commonly asked Unity questions.
If you are a moderator, see our Moderator Guidelines page.
We are making improvements to UA, see the list of changes.
Answers and Comments
4 People are following this question.
Rotating an Object with Touch and drag
How to Make animation Playable?
Rotate a gameobject using 2 fingers (Twist)
Necromancer GUI isn't working?
Flying space game rotate X axis problem