NPC walks through walls

Hi guys,

I’m currently developing a tactical RPG and I’ve set walls in order to prevent the NPC to move outside the game’s area. However, the NPC is moving through the walls. He has a rigidbody, a capsule collider attached to him and he is triggered. Besides, the use gravity option and is kinematic are marked as well. The walls have a rigidbody and a box collider. They are not triggered.

I’m using this function to move the NPC. I call it in FixedUpdate:

public void MoveNPC()
    {
                targetPosition.y = 1.4f;
                targetPosition.x = this.transform.position.x;
                targetPosition.z = this.transform.position.z - movMax;
            myRigidbody.MovePosition(Vector3.MoveTowards(transform.position, targetPosition, velocity));
                IsMoving = true;
    }

The NPC still walks through the walls. How could I fix it?

Hi @jackjohnsm89

A couple of things:

Collider.isTrigger: "A trigger doesn’t register a collision with an incoming Rigidbody. "

rigidbody.isKinematic: "If isKinematic is enabled, Forces, collisions or joints will not affect the rigidbody anymore. "

You’re gonna want to turn both of these off. Make sure the trigger is off on both colliders. It should work. If it still doesn’t work you can look into Raycasting as a means of making your own collision detection.