- Home /
Rigidbody Collision results in object flying away at high velocity
I am trying to make a really simple effect in a flying game, where colliding into a solid object causes the player to "bounce" off of it. I am testing this at low velocity.
I've applied rigidbodies and box colliders to my walls, and since they are solid walls, I've checked all the boxes for freezing position and rotation XYZ. see attached screenshot for all settings.
The character has a simple rigidbody as well; both have the same a basic physics material applied.
When the character hits the wall, at say 2mph, he flies away spinning at something like 20mph.
How can I get it so the character bounces off the wall with the same or less energy that he collides with?
If you are setting the velocity manually rather than applying forces you will get strange effects like objects bouncing too much.
If you set the velocity of an object ins$$anonymous$$d of accelerating it by applying a force, you are essentially accelerating instantaneously and that implies infinite force, hence massive bouncing.
But without knowing your control script I can't say if this is the answer to your problem.
Answer by BroVodo · Apr 24, 2015 at 04:17 AM
Setting the velocity of the RigidBody is what is causing the unrealistic physics effects.
Control the RigidBody by applying forces instead to get realistic results.
In most cases you should not modify the velocity directly, as this can result in unrealistic behaviour. Don't set the velocity of an object every physics step, this will lead to unrealistic physics simulation.
from http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
What is probably happening is that the player is bouncing back on collision then their velocity is being set instantaneously back to their original velocity before impact, resulting in a massive change in acceleration and therefore a massive application of force, which creates the huge bouncing effect, which is multiplied as they hit the obstacle numerous times before the bounce force overwhelms the velocity changes, or the player stops pressing the forward button.
If you set the velocity, it doesn't allow for changes in direction due to collisions because the velocity overrides the velocity changes set by the collision.
Also, if the obstacles in the scene don't move they don't need rigid bodies applied to them (only colliders). If you do need to move them but they don't move on impact, set them to IsKinematic = true, rather than constraining all their axes.
Extra-also, make everything to scale if possible, i.e. 1 unit in unity = 1 meter. Make the mass of the flying robot what you would expect that amount of metal to be in real life. This gives the most realistic results, if that is what you are going for.
@BroVodo, THAN$$anonymous$$ YOU. Your 'bonus' solution actually contained the fix. The SOLUTION IS : RE$$anonymous$$OVE THE RIGIDBODIES FRO$$anonymous$$ THE STATIONARY OBJECTS.
I first changed the explicit velocity application to an intermittent force-only application as both Robotron and you suggested; however the error still occurred. Once I removed the RigidBodies from the wall and bridge though, the physics implemented perfectly. Again, THAN$$anonymous$$S!
Ha! Scattergun answer approach worked! Glad you got it working how you wanted.
Answer by Robotron18 · Apr 22, 2015 at 02:30 PM
Rigidbody.drag must to help you. http://docs.unity3d.com/ScriptReference/Rigidbody-drag.html
First try to regular mass and grad into your rigidbody component. Then try to change Physic Material in your colliders.
At last you can use script with OnCollisionEnter in colliders game objects http://docs.unity3d.com/ScriptReference/Collider.OnCollisionEnter.html
and change Rigidbody.velocity then collision with wall. http://docs.unity3d.com/ScriptReference/Rigidbody-velocity.html
@Robotron18, I get the drag component... but I already have some drag and don't want to introduce more... think more like a zero G sim... robot floats into a heavy space station, robot bounces off space station... zero drag. The thing is, clearly energy is being injected into the collision. I thought Unity ran off a true physics (nVidia PhysX) model... if so, energy should be conserved. I simply want the robot to bounce off the wall at a reasonably, believable velocity
I've appended the original question with screenshots for clarification
If you edit the Rigidbody.velocity, so dont hope for normal work of the physics. Incorrect behavior of physics is because at the time of the collision you change the speed setting established physics.
Use ConstantForce.relativeForce and ConstantForce.relativeTorque for move robot. We must change sign of ConstantForce only one time, not in Update() (every frame). And dont forget set Rubber into the Physic $$anonymous$$aterial in the walls.
http://docs.unity3d.com/ScriptReference/ConstantForce.html
PS Unfreeeze position and rotation. PSS If you want example of ConstantForce code, let me know.
I think he has frozen the position and rotation on the building, not the robot. He might have done it on both tho...