- Home /
Object rotates from Y velocity on Collision
I have a simple plane and a cube in my scene, the cube has a rigidbody + box collider.
I have this script attached to the cube
function OnCollisionEnter(collision: Collision) {
rigidbody.velocity = rigidbody.velocity + Vector3(0,10,0);
}
On every "bounce" the cube starts rotating, this is indicated by the trail renderer I've attached.
Why do you guys think its rotating? I cant seem to figure it out.
Any help is appreciated, thanks!
Answer by ScroodgeM · Aug 31, 2012 at 09:46 PM
physic calculations are mostly unpredictable. even in real life you will never get an ideal cube and ideal plane to jump directly in ideal direction.
you talking about rotating, but i see a moving on your screen.
anyway, to freeze rotations use rigidbody's properties
Thanks for answering Scroodge!
I know that physics is unpredictable but something as straight forward as adding velocity on Y shouldnt do this unless some other factors are introduced?
I've actually locked the rotations however it still rotates.
I used: rigidbody.constraints = RigidbodyConstraints.FreezeRotationX; rigidbody.constraints = RigidbodyConstraints.FreezeRotationY; rigidbody.constraints = RigidbodyConstraints.FreezeRotationZ;
The block rotates, its moving because its edge hit the plane and moved in the direction it was angled.
rigidbody.constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ;
will be correct
Answer by fabianzaf · Aug 31, 2012 at 10:26 PM
I've figured it out!
I had to go to edit > project settings > Physics
In the inspector you have to set Max Angular Velocity to 0, it stops it from rotating, not sure what the side effects are but at least it solves this problem.
you'd disable all rotations in scene. of course this can be a solution, if you really needn't any rotations.
Oh my you're right, either way it lets me do something I wanted in an experiment. Thank you for your help!
What I can do is programmatically set the $$anonymous$$ax Angular Velocity with Rigidbody.maxAngularVelocity inside onCollisionEnter then on exit I can set it back to where it used to be.
freeze rotation for your object only. you can do it programmatically (see below) or in rigidbody's properties
Your method is probably more elegant, I'll mark your answer as correct