Player Shooting.. how can I stop player shooting when in a NPC or area in range

Hi, I followed the Unity tutorial ‘Survival Shooter’, but in my own game wanted to adapt it so the player doesn’t shoot when in a certain area especially in a NPC’s range where I want to click an NPC to advance dialogue, but for now my Player is so happy go lucky with the gun and shoots whenever i click the mouse button …and therefore any other button on the ui screen, is there a way to disable left mouse on click when i want so that i can choose a ui button instead, any help would be really grateful…

using UnityEngine;

public class PlayerShooting : MonoBehaviour
{
public int damagePerShot = 20;
public float timeBetweenBullets = 0.15f;
public float range = 100f;

float timer;
Ray shootRay = new Ray();
RaycastHit shootHit;
int shootableMask;
ParticleSystem gunParticles;
LineRenderer gunLine;
AudioSource gunAudio;
Light gunLight;
float effectsDisplayTime = 0.2f;

void Awake()
{
shootableMask = LayerMask.GetMask(“Shootable”);
gunParticles = GetComponent();
gunLine = GetComponent();
gunAudio = GetComponent();
gunLight = GetComponent();

// Debug.DrawLine(shootRay.origin, shootRay.direction * 100, Color.red, 15f);
}

void Update()
{
timer += Time.deltaTime;

if (Input.GetButton(“Fire1”) && timer >= timeBetweenBullets && Time.timeScale != 0)
{
Shoot();
}

// If the timer has exceeded the proportion of timeBetweenBullets that the effects should be displayed for…
if (timer >= timeBetweenBullets * effectsDisplayTime)
{
// … disable the effects.
DisableEffects();
}
}

public void DisableEffects()
{
gunLine.enabled = false;
gunLight.enabled = false;
}

void Shoot()
{
timer = 0f;

gunAudio.Play();

gunLight.enabled = true;

gunParticles.Stop();
gunParticles.Play();

gunLine.enabled = true;
gunLine.SetPosition(0, transform.position);

shootRay.origin = transform.position;
shootRay.direction = transform.forward;

if (Physics.Raycast(shootRay, out shootHit, range, shootableMask))
{
EnemyHealth enemyHealth = shootHit.collider.GetComponent();
if (enemyHealth != null)
{
enemyHealth.TakeDamage(damagePerShot, shootHit.point);
Debug.Log(“Gun is damaging”);
}
gunLine.SetPosition(1, shootHit.point);
}
else
{
gunLine.SetPosition(1, shootRay.origin + shootRay.direction * range);
}

}
}

I think the easiest way to do this is to first register if any NPC is around and we are close to it. You could do this with Physics.OverlapSphere (the radius is how you could define the distance to the NPC) and giving the NPC a proper tag. As soon as the NPC enters the sphere you could switch a bool (for example canShoot) to false and add
if (Input.GetButton(“Fire1”) && timer >= timeBetweenBullets && Time.timeScale != 0 && canShoot)
{
Shoot();
}

else if (Input.GetButton(“Fire1”) &&! canShoot)
{
InteractWithNPC();
}

thx for getting back so quick! i will try it :smile: