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