- Home /
Changing body type right before the collision response
When a collision occurs between a kinematic body and a dynamic body, I want to set the kinematic body to dynamic before the collision is resolved. I want to do this because otherwise the dynamic body stops momentarily as the kinetic body consumes dynamic bosy's energy..
I want the collision to behave as if that kinematic body was always a dynamic body. Other than adding a fake force OnCollisionEnter to speed up both bodies, I couldn't find a good way to do this. (This fake force is hard to tweak because of dragging forces, etc.. and again it still is not as smooth as dynamic to dynamic collision)
(I am doing these for enabling child rigid bodies on that kinematic body to represent a destructible structure)
How to solve this issue?
Answer by FM-Productions · May 28, 2017 at 07:22 PM
Why not making your kinematic body dynamic in the first place? Does it have to be kinematic?
You could make a simple collision test beforehand. Maybe adding an invisible sphere to object1 and an invisible sphere to object2. Those spheres are the "bounds" of your 2 objects, if they intersect/collide, set the kinematic body to dynamic. If they are not colliding anymore, reset it to kinematic. You could add sphere colliders that are set to trigger and set their parent transforms to the transforms of the according objects (object1 and object2). Of course, the bounds do not have to be spheres, it was just an example. The basic idea is to have a collision detection for the bounds of the objects and then a more detailed collision detection between the objects itself. That way you can set attributes of the objects when they are already near each other, but maybe not colliding yet.
This is part of a destructible system I am working on, where each part that is going to fall off is a kinematic body. If I make it dynamic in the first place, then the destructible (for example a building) will fall of without getting any damage.
If I add invisible colliders (sensors), the problem with that is: - If I make it big, there may be false positives related to the actual collision as the dynamic body may stop moving and may not hit the actual kinematic body even if the invisible colliders hit each other. - If I make it small, I may not have enough time to change bodies to kinematic as the collision of sensors and collision of kinematic-dynamic bodies may happen in the same timestep (physics step).
Am I wrong?
Ok, now I know what you are ai$$anonymous$$g to do. You are right with the false positives. But I have another suggestion:
Have the destructible objects as one single gameObject (not as single parts with a kinematic rigidbody) in the scene. This way you can have your destructible object either as kinematic or dynamic rigidbody. If a collision is detected, simply exchange the destructible object with the single parts of the object, something like this:
https://www.youtube.com/watch?v=EgNV0PWVaS8
You would, of course, still have to apply the right forces to the single parts after the exchange. $$anonymous$$aybe you could set the weight of the whole destructible object very low, so the impact does not actually stop the projectile from moving towards the destructible object. This way you can detect further collisions after you replaced the whole destructible object with its parts.
Another suggestion for your destructible object: define 2 separate colliders for the destructible object. One that is not a trigger and that only collides with the character, so that it prevents the character from moving or walking through the destructible object. Set up another collider, set it to "isTrigger" and only detect collisions with that collider against projectiles. This way, a collision between a projectile and a destructible object will have no force impact on the projectile and it does not change its force or speed. After you have noticed the collision with OnTriggerEnter (https://docs.unity3d.com/ScriptReference/$$anonymous$$onoBehaviour.OnTriggerEnter.html) you swap the destrutible object with it's parts. The parts have regular colliders (isTrigger = false) and react to the force of the projectile.
Here is the link about layer based collision detection:
https://docs.unity3d.com/$$anonymous$$anual/LayerBasedCollision.html
Apparently, there is also a rigidbody.Sleep() function, which might work for your needs. I have no experience with this function but I have read about it in this thread, which has an answer quite similar to yours I would say:
http://answers.unity3d.com/questions/11866/shattering-glass.html
Your answer
Follow this Question
Related Questions
Parent rigidbody using its childrens colliders. Should it work or am I stupid? 2 Answers
Colliding fast moving object with a slow moving object 1 Answer
Any way to ignore collision between rigidbodies and colliders/character controllers? 1 Answer
Fixed Joint - Collision Problem 1 Answer
Character Controller meets Rigidbody 1 Answer