Multiple raycasts from one gameobject

Hi everyone, Im trying to recover some information about other gameobjects from multiple raycasts being cast from one specific gameobject. But the problem is that it only runs the first raycast when cleary (after drawing the other raycasts) the other raycasts intersect the other gameobject but nothing happens and I dont understand why. I already searched on the matter but still I cant understand why my code isn’t working.

Basically I am offsetting the angle of the original raycast going forward. But the offsetting raycasts arent working…

private int GetPlayerAttackedID()
    {
      
        RaycastHit hit;
        RaycastHit hit2;
        RaycastHit hit3;
        RaycastHit hit4;
        RaycastHit hit5;

        Vector3 step = new Vector3(0, 1, 0);
        // Spread angle of raycasts.
        Quaternion spreadAngleNegative =  Quaternion.AngleAxis(-60.0f, new Vector3(0, 1, 0));
        Quaternion spreadAnglePositive =  Quaternion.AngleAxis(60.0f, new Vector3(0, 1, 0));

        Quaternion spreadAngleNegative2 =  Quaternion.AngleAxis(-30.0f, new Vector3(0, 1, 0));
        Quaternion spreadAnglePositive2 =  Quaternion.AngleAxis(30.0f, new Vector3(0, 1, 0));

        // Draw raycasts for debugging.
        //Debug.DrawLine (transform.position + step, transform.TransformDirection(Vector3.forward) * 6, Color.red);
        Debug.DrawLine (transform.position + step, transform.TransformDirection(spreadAngleNegative * Vector3.forward) * 6, Color.blue);
        Debug.DrawLine (transform.position + step, transform.TransformDirection(spreadAnglePositive * Vector3.forward) * 6, Color.blue);

        Debug.DrawLine (transform.position + step, transform.TransformDirection(spreadAngleNegative2 * Vector3.forward) * 6, Color.yellow);
        Debug.DrawLine (transform.position + step, transform.TransformDirection(spreadAnglePositive2 * Vector3.forward) * 6, Color.yellow);


        if(Physics.Raycast(transform.position + step, transform.TransformDirection(Vector3.forward), out hit))
        {
            if(hit.distance < 6 && hit.transform.tag == "Player")
            {
                    Debug.LogWarning("it came from 1");
                    return hit.transform.gameObject.GetPhotonView().ownerId;
            }
        }

        else if(Physics.Raycast(transform.position + step, transform.TransformDirection(spreadAngleNegative * Vector3.forward), out hit2))
        {
            if(hit2.distance < 10 && hit2.transform.tag == "Player")
            {
                Debug.LogWarning("it came from 2");
                return hit2.transform.gameObject.GetPhotonView().ownerId;
            }
        }

        else if(Physics.Raycast(transform.position + step, transform.TransformDirection(spreadAnglePositive * Vector3.forward), out hit3))
        {
            if(hit3.distance < 10 && hit3.transform.tag == "Player")
            {
                Debug.LogWarning("it came from 3");
                return hit3.transform.gameObject.GetPhotonView().ownerId;
            }
        }

        else if(Physics.Raycast(transform.position + step, transform.TransformDirection(spreadAngleNegative2 * Vector3.forward), out hit4))
        {
            if(hit4.distance < 10 && hit4.transform.tag == "Player")
            {
                Debug.LogWarning("it came from 4");
                return hit4.transform.gameObject.GetPhotonView().ownerId;
            }
        }

        else if(Physics.Raycast(transform.position + step, transform.TransformDirection(spreadAnglePositive2 * Vector3.forward), out hit5))
        {
            if(hit5.distance < 10 && hit5.transform.tag == "Player")
            {
                Debug.LogWarning("it came from 5");
                return hit5.transform.gameObject.GetPhotonView().ownerId;
            }
        }
     
        return 0;
    }

All your other raycasts are else cases, and if it got inside the first if but failed the inner if, it would still count as happening and therefore the others won’t. So if it hits something that’s more than 6u away and/or not the player, it still skips the others.

Since you’re returning the result on success, I think you can remove the else - just have five if blocks in a row.

You may want to try using the predefined constant Vector3.up in place of those new Vector3(0, 1, 0) calls. It’s more readable and should avoid runtime allocations. :slight_smile:

1 Like