Home Game Development Raycasting unable to detect water layer

Raycasting unable to detect water layer

0
Raycasting unable to detect water layer

[ad_1]

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:

[SerializeField]
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.

[ad_2]

Previous article Party Games: Bringing People Together
Next article Blizzard Rushing To Fix Broken Glyph XP From Patch 1.2.3
Hello there! My name is YoleeTeam, and I am thrilled to welcome you to AmazonianGames.com. As the premier destination for all things related to Amazon Games' universe, we are dedicated to providing you with the most exciting and immersive gaming experiences out there. From captivating visuals to exhilarating gameplay, our website is packed with comprehensive insights, updates, and reviews to keep you ahead of the game. Whether you're a seasoned gamer or new to the scene, I am here to guide you through this virtual frontier. If you have any questions or suggestions, feel free to reach out to me at john@yoleesolutions.com. Embark on your Amazon gaming journey today with AmazonianGames.com and let the adventure begin!