- Home /
Freezing X and Y rotation not working (rigidBody)
Hi,
I'm trying to rotate a gameObject around its Z axis and want to freeze the X and Y axis. I've done this in the rigidBody constraints but after a some time or a few collisions the object starts spazzing out in the frozen axes.
This is the code I'm using for the rotation of the gameObject:
if (Input.GetMouseButton(1) == true || Input.GetMouseButton(0))
{
if (Input.GetMouseButton(1))
{
GetComponent<Rigidbody>().AddTorque(Vector3.back * manoeveringThrust * Time.deltaTime);
}
if (Input.GetMouseButton(0))
{
GetComponent<Rigidbody>().AddTorque(Vector3.forward * manoeveringThrust * Time.deltaTime);
}
}
I'm using AddTorque because this is a space game and I want it to feel floaty and to go with the general theme of the game of using force to simulate gravity and such. The player looks at the game from a 2D perspective so whenever the player starts rotating in the X or Y axes he gets stuck.
Am I understanding/doing something wrong with these rigidBody constraints and is there a way to forcefully freeze those rotations? Ideally I'd want to just force the X and Y rotations to be 0 at all times.
Answer by M-Hanssen · May 09, 2016 at 08:44 AM
Try freezing the rotation manually in the LateUpdate
protected void LateUpdate()
{
transform.localEulerAngles = new Vector3(0, 0, transform.localEulerAngles.z);
}
Thanks! This solved my issue as well, but in my case I was trying to stop all z-axis rotations for good. =]
Your answer
Follow this Question
Related Questions
How can I constrain physics in all directions except one? 2 Answers
Why are a rigidbody's freeze constraints not working? 2 Answers
Smoothed Particle Hydrodynamics 0 Answers
Why is there no Rigidbody2D.SweepTest() ? 1 Answer
Running on walls 1 Answer