- Home /
Rotate a rigidbody that has frozen rotation
Hi all. So I've frozen rotation on my RigidBody because I don't want it spinning out of control when colliding with objects at high speeds, but I still want to be able to rotate it myself. When using the freeze checkboxes, it no longer rotates when I use the following code:
float roll = Input.GetAxis ("Roll") * rollSensitivity * Time.deltaTime * acceleration;
rb.AddTorque (transform.forward * roll);
My question is, does anybody know a way to make it roll even with the rotation set to frozen, OR, disable rotation from collision? Thanks.
why are you freezing it ? You can alternatively simply get it's rotation restricted to the extent you want ? rb.$$anonymous$$oveRotation($$anonymous$$athf.Clamp(rb.rotation, -45, 45));
I can't do that because this is a space game where I can rotate every direction. Clamping it would mean that I can't rotate anywhere.
I freeze it so it doesn't rotate on bounce but then if I unfreeze it it rotates on bounce. Freezing it also stops my movements on it.
You could probably just cache your angular velocity every frame ins$$anonymous$$d of freezing anything. In OnCollisionEnter, just overwrite the new angular velocity with the cached copy, and it'll be like the collision didn't happen in that respect only. Defer the overwrite to LateUpdate to be sure that you're overwriting it AFTER it's been changed by the collision though- I'm not sure OnCollisionEnter is guaranteed to fire after the new velocity is calculated.
You'll likely have lingering issues if contact with another object is for more than a single frame, but that should get you closer to a solution at least.
Thankyou very much Lysander! I will try this now.
Hmm it doesn't seem to do anything. I'm setting a boolean to change in OnCollisionEnter and then replacing it in LateUpdate but still doesn't seem to care about the velocity change.
$$anonymous$$ight try delaying it an entire frame- start a coroutine, use WaitForFixedUpdate once (or twice possibly) and overwrite it there. Use Debug.Logs and see if you can pin down exactly where the angular velocity is being recalculated from the collision. This isn't something I've ever had to do before, so I can't really give you much better advice.
Answer by OneCept-Games · Jan 02, 2018 at 11:57 AM
You have conflicting requests. Freezing an objects rotation in one or more axis is taking into account in all Rigidbody physics methods, like AddTorque() etc. In this case, you should rotate your Transform directly by Transform.Rotate(). Doing this, it might cause "bullet" issues with other objects colliding with the object you are rotating by force, so depending on your context, be aware of mixing Rigidbody methods with methods transforming your objects directly - Physics- and Direct transform are not good friends.
I understand but I use the AddTorque() for the express reason of being able to have it happen over time consistently. Using Transform.Rotate() would mean its a one-off rotation.
It's not a conflicting request per se, rather I simply want to be able to use RigidBody rotations without having any collisions with the environment affect the rotation. I want it to collide, not start spinning everywhere.