How do I make raycast ignore the players collider

i’m having a problem where I can’t shoot when moving forward/backward because all the raycasts hit inside the players collider, how can I make the raycast ignore the layer that the player is using?

my shoot script:

using UnityEngine;

public class gunscript : MonoBehaviour
{

    public float damage = 10f;
    public float range = 100f;
    public float impactForce = 20f;
    public float fireRate = 15f;
    public AudioSource audioSource;


    public Camera fpsCam;
    public ParticleSystem muzzleFlash;
    public GameObject impactEffect;
    public LayerMask PlayerLayerMask;

    private float nextTimeToFire = 0f;

    void Update()
    {
        if (Input.GetButton("Fire1") && Time.time >= nextTimeToFire)
        {
            nextTimeToFire = Time.time + 1f / fireRate;
            Shoot();
        }
    }

    void Shoot()
    {

        RaycastHit hit;

        muzzleFlash.Play();

        audioSource.Play();

        if (Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range))
        {
            target target = hit.transform.GetComponent<target>();
            if (target != null)
            {
                target.TakeDamage(damage);
            }

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

            GameObject impactGO = Instantiate(impactEffect, hit.point, Quaternion.LookRotation(hit.normal));
            Destroy(impactGO, 2f);
        }
    }
}

thanks in advance

You already have a LayerMask property - use it!

When you pass a LayerMask to a raycast method call, the raycast will only hit colliders on the specified layers and ignore all others.

Edit for further explanation. Here’s the reference page for Physics.Raycast: Unity - Scripting API: Physics.Raycast

Note that…

  1. there’s different versions (“overloads”) that have different parameters.
  2. some parameters have default values (like float maxDistance = Mathf.Infinity).

That means you have different ways to raycast, and you can use more or fewer parameters. You need an overload that has the layerMask parameter. Since your code requires the out hit parameter as well, you need an overload that has both. This means that this one is for you:

public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hitInfo, float maxDistance, int layerMask, QueryTriggerInteraction queryTriggerInteraction);

So you change your raycast line to have these parameters in there…:

Physics.Raycast(fpsCam.transform.position, fpsCam.transform.forward, out hit, range, PlayerLayerMask);

Despite the QueryTriggerInteraction parameter having no default value according to this page, I’m pretty sure you can omit it.

You already have a serialized LayerMask property in your code:

public LayerMask PlayerLayerMask;

I’d definitely rename it because the mask is supposed to contain everything that should be hit, so it’s the opposite of the “PlayerLayerMask” :slight_smile:

Remember to actually set the LayerMask up in the inspector after putting your script on a GameObject.