I'm having issues detecting the ocean in my Unity scene (screenshot below) using Raycasting. In the image below, I've attached my script to a child object and set the layer for the Ocean object to Water.

enter image description here

The code looks something like this:

private Transform playerPosition;
private float detectionFeelers = 2f;

// Update is called once per frame
void Update()
    RaycastHit hit;
    Vector3 raycastOrigin = playerPosition.position; 

    //tried LayerMask.GetLayer("Water") and using 
    if (Physics.Raycast(raycastOrigin, Vector3.down, out hit, detectionFeelers, 1<<4))
        // Player is near the water
        Debug.Log("Player is near the water! Hit object: " + hit.collider.gameObject.name + " Distance: " + hit.distance);


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

I've also tried selecting the layer for the Physics.Raycast function by using the dropdown option and by getting the layer by using GetLayer(), and I've given the ocean object a mesh collider as well but nothing works. I just can't detect proximity to the ocean.

Any help would be much appreciated.



You must log in to answer this question.

Browse other questions tagged .