Make RayCast Ignore Player & Gun

I am looking for help on my FPS project. I have a shooting system that uses RayCasting. It comes from the FPS cam which is inside the player. It works but occasionally will hit either the gun or my arms if I move certain directions/ways. I have changed the point of origin to a separate point that is just beyond the camera, but moving that too far out to avoid hitting the gun and arms creates its own problem. How do I make the raycast simply ignore the gun and arms. I have tried numerous methods I have found, the “best” one seeming to be to add a LayerMask to the Physics.Raycast(), however that does not work. The arms are on their own layer as are the guns, yet any layermask I select in the inspector outside of “Nothing”, the gun and arms will be hit. I have changed the settings in the unity project settings as one video suggested, that does nothing either. Any feedback would be great, thank you. Code Below:

RaycastHit hit;

if (Physics.Raycast(bulletPointOfOrigin.transform.position, bulletPointOfOrigin.transform.forward, out hit, layerMask))
        {
            Debug.Log(hit.transform.name);
             hit.transform.SendMessage("HitByRay");
           
            Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));

         
                  if(hit.transform.tag != "Glass")
            {
                Instantiate(bulletHole, hit.point, Quaternion.LookRotation(hit.normal));
            }

            if(hit.transform.tag == "Glass")
            {
                Instantiate(glassBulletHole, hit.point, Quaternion.LookRotation(hit.normal));
                FindObjectOfType<AudioManager>().Play("GlassShot");
            }
            if (hit.transform.tag =="ExplosiveBarrel")
            {
                Instantiate(barrelExplosion, hit.point, Quaternion.LookRotation(hit.normal));
                FindObjectOfType<AudioManager>().Play("BarrelExplode");
            }

            if (hit.rigidbody != null)
            {
                hit.rigidbody.AddForce(-hit.normal * impactForce);
            }
        
             
        }

You’re not using it right. Check the documentation, you’re putting your layermask in the parameter for “maxDistance”.

Oh jeez that was simple, my bad. Thank you!

Yes it is confusing because both params accepts number so it won’t throw compile error…

1 Like

Yeah true

Personally I got into the habit of using named arguments for things that have overloads or multiple same typed arguments such as this. It’s not for everyone but it certain can be useful.

2 Likes

Agreed. I think it is critical for any function that either:

a) has multiple arguments of the same type
b) has multiple overloads that change argument order.

Failure to use named arguments in the above cases is simply setting yourself up for failure. This is not a good strategy.

Always use named arguments with Physics.Raycast() because it contains many poorly-designed overloads:

https://discussions.unity.com/t/758115/8

1 Like