Raycast can't detect interaction with layer

Hey everyone,

I am trying to detect proximity between an object and the ocean object in my game using Raycasting. However, even after taking a look at similar code from other forums, I cannot detect any interaction. Any help with this would be much appreciated as I’m a beginner in both HDRP and Unity 3D. Here is my code:

[SerializeField]
    private Transform playerPosition;

    //this may be adjusted depending on the speed and size; research required
    private float detectionFeelers = 2f;

    // Update is called once per frame
    void Update()
    {
        RaycastHit hitWater;
        Vector3 raycastOrigin = playerPosition.position; //may need some offset

        if (Physics.Raycast(raycastOrigin, Vector3.down, out hitWater, detectionFeelers, LayerMask.GetMask("Water")))
        {
            // Player is near the water
            Debug.Log("Player is near the water! Hit object: " + hitWater.collider.gameObject.name + " Distance: " + hitWater.distance);
        }
    }

    private void OnDrawGizmos()
    {
        Vector3 gizmoPosition = transform.position;
        Gizmos.color = Color.red;
        Gizmos.DrawRay(gizmoPosition, Vector3.down * detectionFeelers);
    }

if your player is within the 2f distance of your ocean it should detect the ocean. if;

  • Your ocean layer is set both in mask and the ocean object.
  • Your ocean has a collider. Raycasts hit colliders so you need a collider for a raycast.

Also your raycast direction is directly below your player. is that intentional ?