Does anyone out there in the Unityverse know how I can freeze rotation for a rigid body on just one of its axes?
Could do the following.
Dont know what exactly you are doing, but usually fixing rotation on one axis is not the best idea.
If you can, it usually works better to simply fix the whole rotation of the rigidbody using freezeRotation and drive the rotation completely through scripts.
enum FixAxis { X = 0, Y, Z }
var fixAxis = FixAxis.Z;
private var originalAngles = Vector3.zero;
@script RequireComponent(Rigidbody)
function Start ()
{
originalAngles = transform.eulerAngles;
}
function FixedUpdate () {
if (fixAxis == FixAxis.X)
{
transform.eulerAngles.x = originalAngles.x;
rigidbody.angularVelocity.x = 0.0;
}
else if (fixAxis == FixAxis.Y)
{
transform.eulerAngles.y = originalAngles.y;
rigidbody.angularVelocity.y = 0.0;
}
else
{
transform.eulerAngles.y = originalAngles.y;
rigidbody.angularVelocity.y = 0.0;
}
}
Yeah, I definitely don’t want to do it in code if I’ll be fighting the Aegis engine every step. I was just hoping the freeze property might have an undocumented freeze by axes option.
It would be a nice addition, if you’re looking for suggestions for the next rlease!
Sure would be nice.
Just wondering what exactly you want to do. Because in 99% of all player controlled crafts / vehicles etc. it feels better to have full scripting control over the rotation.
Eg. in a spaceship, only because you hit that rock doesn’t mean you want to go off spinning in a physically accurate way. Depends on what you are after of course.
You pretty much hit the nail on he head there - I want to be able to control the (sometimes overzelaous) physics of rotation after collisions.
I’m working on a little project right now where I have objects colliding with each other and I want to let the physics engine move them and rotate them relative to the plane that hey are sitting on. To that end, while I want the organic look of the engine’s physics, I want rotation locked to Y-axis only so that “hit” objects just spin on Y, rather than tumbling on XYZ.
What if you let PhysX handle it, but nullify eveything but Y spin using rigidbody.angularVelocity and transform.rotation?
You could check every update for a non-zero velocity and non-zero rotation and make them equal to zero.
Or, it might be neat to let the ship get knocked a bit but lerp it back to level. Lerp the angularVelocity towards zero, and then when it gets close to zero, start lerping transform.rotation as well. You could have as much or as little non-Y “wobble” as you wished.
Can’t you just detect when a collision happens, (Only big bounces not small ones of course) and then manually rotate around the y-axis using transform.Rotate.
Gives you much more control and you don’t need all the hassle applying the torque in a sensible way.
Although the docs suggest it is bad form, i have found simply forcing the angular velocities of the axes you want frozen to zero works fine. It does seem to work better than having physics conflicting with setting the rotation in the transform rather than the rigidbody.