After a certain amount of time raycast ignores a hit with an enemy

I have a game with a gun, which activates a raycast to detect collisions whilst firing. If the raycast collides with an enemy transform , the enemy will take 10 damage (enemy has 100 base health) until it gets destroyed after around ten hits…

Once I was play-testing, and a sum of time passed since the first enemies spawned, I realised that the raycast started to ignore detecting those enemy and detected the ground instead in the console menu (basically the raycast went straight through the enemy and detected the ground).

Im not sure why the raycast is going straight through the enemy transform and is stopping at the ground instead, especially when this behaviour only happens after around 8 seconds. Does anybody know why this happens?

Plus, this only started happening once I gave the enemies a script and NavMesh Agent to give them AI to follow the player’s cylinder position, so that might be linked to this problem.

This is the gun and enemy damage code (from a tutorial):

using UnityEngine;

public class Gun : MonoBehaviour
{
    public float damage = 10f;
    public float range = 100f;
    public float fireRate = 15f;

    public Camera fpsCam;
    public ParticleSystem muzzleFlash;

    private float nextTimeToFire = 0f;

    // Update is called once per frame
    void Update()
    {
        if(Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f / fireRate;
            Shoot();
        }
    }

    void Shoot ()
    {
        muzzleFlash.Play();

        RaycastHit hit;
        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            Debug.Log(hit.transform.name);

            Enemy enemy = hit.transform.GetComponent<Enemy>();
            if(enemy != null)
            {
                enemy.TakeDamage(damage);
            }
        }
    }
}

The Enemy enemy = hit transform.GetComponent part of the code is getting the script I believe from the actual enemy (The Enemy’s script is called Enemy).

using UnityEngine;

public class Enemy : MonoBehaviour
{
    public float health = 100f;

    public void TakeDamage (float amount)
    {
        health -= amount;
        if (health <= 0f)
        {
            Die();
        }
    }
 
    void Die ()
    {
        Destroy(gameObject);
    }

    private void OnCollisionEnter(Collision collision)
    {
        if(collision.gameObject.tag == "Player")
        {
            Destroy(collision.gameObject);
        }
    }

}

This is also the EnemyFollow code just incase it is needed;

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class EnemyFollow : MonoBehaviour
{
    public NavMeshAgent enemyAI;

    // Start is called before the first frame update
    void Start()
    {
     
    }

    // Update is called once per frame
    void Update() 
    {
        enemyAI.SetDestination(GameObject.Find("Cylinder").transform.position);
    }
}

Nevermind, I fixed this by turing is Kinematic on for the enemies.

For anyone curious: The reason for this happening is that there is nothing physical to stop the rigidbodies accelerating under gravity, but the navigation system keeps forcing the position of the enemy back to the surface. This means that at first, the update loop does this:

Enemy: <doesn’t move>
Raycast:
Navmesh:

Enemy:
Raycast:
Navmesh:

Enemy: <moves down a little more, since it’s still accelerating under gravity>
Raycast:
Navmesh:

But after a while:

Enemy:
Raycast:
Navmesh:

Enemy:
Raycast:
Navmesh:

So you never see the enemy falling, but the physics constantly misses.