- Home /
Stop Rigidbodies From Overlapping
Okay I have a script which allows objects to be moved with the mouse. The objects have rigidbodies and whenever you place an object on another object they can overlap and cause the object to fly everywhere or get stuck. How can i make it so the rigidbodies/collider do not overlap eachother whenever i try to a object inside another object
have you tried http://unity3d.com/support/documentation/ScriptReference/Physics.OverlapSphere>OverlapSphere?. This returns true if the sphere radius overlaps another. Ive done this check for spawning with rigidbodies, maybe itll help you in ur current situation
learn about LAYERS
it is a huge innovation Unity3D introduced to solve this sort of problem
Answer by TomasRiker · Jun 23, 2012 at 07:23 AM
The problem is that all the "static" checks are limited to rays, spheres and capsules and can't do complex shapes. So you have to let pass one frame in order to see if something happened.
Here's my idea:
If the user dragged an object using the mouse or placed an object, mark this object somehow (i.e. set a boolean flag to true). Implement OnCollisionStart
for the dragged/newly created object. If this function gets called and your boolean flag is set to true
, you know that something "went wrong". If nothing gets called, set the flag to false
in the first FixedUpdate
.
Now you can detect that a dragged or newly placed object overlaps with some other object. You still have to react to that. A possible way would be to reset both objects' velocity and angular velocity to their old values to prevent them from "exploding" and moving one or both objects along the normal vector of the collision, trying to get them apart.
It sorta works, but not real smoothly because sometimes the object will just get stuck in the air not sure why, but then again it'll do. Thanks!
Answer by Loius · Jun 24, 2012 at 06:44 PM
Move solid rigidbodies by changing their velocity instead of changing their position.
function Update() {
...
heldObject.rigidbody.velocity = (mousePosition - heldObject.transform.position).normalized * desiredMoveSpeed;
}
Wow this really just finishes $$anonymous$$asRiker idea! Thanks for the share with combined ideas they fit correctly EXACTLY what i need!! You guys are both right
Answer by BlacKcuD · Mar 22, 2013 at 12:47 AM
Got to "Edit - Project Settings - Physics" and set the "Min Penetration Penalty" to a ridiculous small number, e.g. 0.001
If happen to see this, do you know what the fix would be now in 2019? It seems there is no longer a '$$anonymous$$in Penetration Penalty.'
Answer by MHUnity · Nov 17, 2016 at 05:49 PM
Another option can be that you can set your RigidBody collision Detection mode to "Continuous" or "Continuous Dynamic" for fast objects.
Your answer
Follow this Question
Related Questions
How to stop rigidbody.sleep() 2 Answers
Two rigidbody's overlap instead of colliding 1 Answer
swapping out an object on collision 2 Answers