This week I needed to fix my enemy movement. Well, the enemy does move, but occasionally the enemy will clip the floor and get some kind of phantom force applied to them making it shake and slowly shimmy away from the player. I believe this is because once the entity collides with a physical object the rigidbody is given force in the opposite direction to the navmesh movement.
This causes the entity to want to move in two opposite directions and due to the way the navmesh controls movement, the entity shakes back and forth on the spot. My immediate thought for a solution was to turn the collider on the enemy into a trigger. Which worked, but in classic bug fixing fashion, it broke something else. For some reason unknown to me, my trigger colliders started calling both their OnTriggerEnter and their OnTriggerExit functions in the one frame. This became a very large issue for another part of the project, with then prevented me from using this very simple solutions.
So, my next thought was, what if we just moved it out of the way? So, I spent some time trying to separate the body from the navmesh agent so that the body could sit higher, until I realised that I could simply increase the height. I was a little upset with myself for missing something so simple. So great, the enemy doesn’t hit the floor and dance any more(I’m a poet and I didn’t even know it!)
My next idea was to stop the collisions from happening. To do this if need to move the centre of the object up so that the collider sat above the point where it would hit steps. However, while this did solve the issue of hitting the floor, the walls still caused this fancy dance to take place.
So I finally gave some proper thought to it and I realised, if it’s an issue with a force being applied to the enemy, rather than trying to prevent the force from being applied when colliding, why don’t I just make the physics of the enemy behave differently. So, I set the rigidbody to iskinematic and voila, parties over, no more dancing.
So, it turns out this issue was indeed caused by the rigidbody. I had a quick search after solving the problem and found that it did indeed need to be “iskinematic” to prevent this issue. I probably should have searched this issue before I tried solving the problem on my own. I would definitely have saved a lot of time.