Collision detection not working when using Nav-mesh

I am working on a game that needs collisions with the player and enemy characters in order to do damage, however I can not get my collisions to work. I have only had this issue when the collision objects have used a nav mesh.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class PlayerStats : MonoBehaviour
{
 [SerializeField] private float maxHealth;
    private float currentHealth;
    public healthBar healthBar;

    private void Start()
    {
        currentHealth = maxHealth;
        healthBar.SetSliderMax(maxHealth);
    }

    public void TakeDamage(float amount)
    {
        currentHealth -= amount;
        healthBar.SetSlider(currentHealth);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if (collision.gameObject.tag == "Enemy")
        {
            TakeDamage(20f);
        }
    }
}

https://docs.unity3d.com/Packages/com.unity.ai.navigation@1.1/manual/MixingComponents.html

  • You don’t need to add physics colliders to NavMesh Agents for them to avoid each other
    • That is, the navigation system simulates agents and their reaction to obstacles and the static world. Here the static world is the baked NavMesh.
  • If you want a NavMesh Agent to push around physics objects or use physics triggers:
    • Add a Collider component (if not present)
    • Add Rigidbody component
      • Turn on kinematic (Is Kinematic) - this is important!
      • Kinematic means that the rigid body is controlled by something else than the physics simulation
  • If both NavMesh Agent and Rigidbody (non-kinematic) are active at the same time, you have race condition
    • Both components may try to move the agent at the same which leads to undefined behavior
  • You can use a NavMesh Agent to move e.g. a player character, without physics
    • Set players agent’s avoidance priority to a small number (high priority), to allow the player to brush through crowds
    • Move the player agent using NavMeshAgent.velocity, so that other agents can predict the player movement to avoid the player.
1 Like