Raycast hit information from a capsule

Hello all. I need your help with some practice with raycast , I have this little piece of code in a capsule with a navmeshAgent. But when I run my script I just have the first collision of the ray, like if i running that piece of code just one time. If it is in the update, doesn’t mean that all time I’ll have the collider name of the things in front of me?(if I’m moving around the enviroment of course). I trying moving my capsule manually around the enviroment but I just have the first ray collision.

public class EstadoPerseguir : MonoBehaviour
{
    private MonoBehaviour Perseguir;
    private MonoBehaviour EnemigoEnRango;
    bool isHitting = true;
    Ray ray;

    [SerializeField] private GameObject enemy;
    RaycastHit hit;


    private void Awake()
    {
        ray.origin = transform.position;
        ray.direction = transform.forward;
    }
    private void Update()
    {
        Physics.Raycast(ray.origin, ray.direction, out hit);

        print(hit.collider.name);

        if (isHitting)
        {
          
            Debug.Log("HITTING " + hit.collider.name);
        }

      
    }
}

This is in front of a “Cube”


This is in front of a “Player”
7723786--969523--upload_2021-12-9_22-10-13.png

Thanks in advance!

Check this critical C# piece of knowledge out:

The difference between Value Types vs Reference Types:

What you do in Awake() is creating a copy of those values, frozen in time.

Move the two lines in Awake to right before you do the cast.

ALSO: Use Physics.Raycast() always with named arguments because it contains many poorly-designed overloads:

I reaaally appreciate the help man, and the explanations. It works, was just that.
Thank you!!

1 Like