- Home /
Rotate 180 degrees over x axis over time
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:
void FixedUpdate()
{
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
}
}
Comment
Answer by BilboBaggins27 · Mar 26, 2017 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'.
Your answer
![](https://koobas.hobune.stream/wayback/20220612113556im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Third Person Controller Rotation 0 Answers
How do I reset the rotation? 1 Answer
Why is the rotation missing when I animate? 1 Answer