- Home /
Kinematic RigidBody does not collide with Wall using MovePosition
I have a kinematic enemy capsule gameobject to which i have added a chase player script. To chase i have used LookAt() to make the enemy object face my player and then used MovePosition() to move it towards my player. The problem is it passes through all the walls I have placed in the way. The walls were static colliders so I assumed that was the reason, i gave a few walls a rigidbody component and added constraints for their position and rotation but the enemy object still passes through the walls. What am I doing wrong?
To summarise, my kinematic rigidbody being moved by a MovePosition function is passing through static colliders as well as non-kinematic rigid body colliders.
The comment in the accepted answer by @mbro514 gives an elegant solution of using character controllers instead of kinematic rigidbodies for the enemy objects
Answer by mbro514 · Jul 21, 2020 at 03:34 PM
Kinematic Rigidbodies will always go through any collider, regardless of what type it is (dynamic, kinematic, or static). If you need an object to collide with other objects, but not be effected by the forces of physics, you could either use a Character Controller, or use a kinematic rigidbody and code a custom collision resolution for it.
A kinematic rigid body does collide with a non-kinematic rigid body. I tested it out. If i removed the constraints i placed on the walls rigid body, the enemy capsule does collide with it but the result of this is that the wall also moves now. $$anonymous$$y doubt here was why does constraining a rigid body cause a kinematic body to go through it, unconstrained rigid bodies do not let kinematic bodies go through them even if they are stationary.
I solved it by making my enemy capsule a rigid body, the only force that could act on it was the projectiles my player shoots so i wrote a script to make the enemy capsule kinematic onCollisonEnter and back to non-kinematic onCollisonExit. That way for the few frames a bullet does hit it, it is kinematic.
Would love a better solution though if possible
The reason why your kinematic rigidbody is pushing your dynamic/non-kinematic rigidbody is this: Your kinematic rigidbody was not stopped by its collision with the dynamic rigidbody, so it penetrated the dynamic rigidbody's collider. However, since dynamic rigidbodies automatically resolve collisions, your dynamic rigidbody pushed itself backwards to resolve its collision with the kinematic rigidbody. This process of penetrating and moving away repeats itself so rapidly that it makes it look as if the kinematic rigidbody is pushing the dynamic one, but it's actually the dynamic pushing itself away. The reason why this pushing wasn't happening when the constraints were on is that the constraints were preventing the dynamic rigidbody from moving to resolve the collision, but the kinematic rigidbody was still able to penetrate it. I hope that this made sense. Anyway, what I advise that you do with your walls is that you make them static, and then do one of two things with your enemy characters: 1) You could give them dynamic rigidbodies, or 2) you could give them character controllers.
Thank you for the explanation! That cleared out why constraints were causing collisions to not happen. Using Character controllers seems like a good way to do this, thank you!
Answer by SkillcraftHD · Jan 18, 2021 at 08:13 AM
You could just increase the mass of the rigidbody infinitily high so it won't be pushed.
True, but I think a safer way would be to use a character controller as mentioned by @mbro514 in the comment above. Character controllers collide with static colliders and do not get affected by the collision forces.