Raycast hits everything on the screen

I want to be able to click on one of the enemies on the screen to Start a Coroutine, but when i test the raycast it hits everything on the screen instead of just hitting the thing i click on which is obviously a problem.

I can get it to tell me in the console that it is hitting the object tagged as “Enemy” but if there are more that one enemies on the screen it hits all of them rather than the one I am clicking on

Here is the script that goes on the enemy prefab:

using UnityEngine;
using System.Collections;

public class NPC : MonoBehaviour
{

public GameObject HealthPack;
private int moveSpeed = 1;
private MovementSpeedManager moveSpeedA;
public int Health = 1;
public Rigidbody coin;
private bool coinHasSpawned = false;
public float coinForce = 1f;
private PlayerGold goldGain;
private PlayerGold totalGold;
public int totalGoldNew;
private int spawnHealthPackNumber;
public Damsell rageMode;

private void Awake()
{
    goldGain = GameObject.Find("GoldManager").GetComponent<PlayerGold>();
    totalGold = GameObject.Find("GoldManager").GetComponent<PlayerGold>();
    moveSpeedA = GameObject.Find("MovementSpeedManager").GetComponent<MovementSpeedManager>();
}

private void Start()
{
    totalGold.totalGold = PlayerPrefs.GetInt("TotalGoldNew");

    moveSpeed = moveSpeedA.moveSpeedA;
}

void Update ()
{

    transform.Translate(-Vector3.forward * moveSpeed * Time.deltaTime);
    

    if (Health <= 0 && coinHasSpawned == false)
    {
        moveSpeed = 0;
        Destroy(GetComponent<Collider>());
        Destroy(this.gameObject, 1f);
        spawnCoin();
        coinHasSpawned = true;
        spawnHealthPack();
    }
    
    if (Input.GetKeyDown(KeyCode.Mouse0))
    {
        RaycastHit hit;
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out hit, 100))
        {
            if (hit.collider.tag == "Enemy")
            {
                Debug.Log(hit.collider.tag);
                StartCoroutine(knockback());
            }  
        }
    }       
}

void OnTriggerEnter(Collider other)
{
    if (other.gameObject.CompareTag("Player"))
    {
        other.GetComponent<PlayerHealth>().playerHealth --;
    }
}

private void spawnCoin()
{
    Rigidbody coinInstance = Instantiate (coin, transform.position, transform.rotation) as Rigidbody;
    coinInstance.AddRelativeForce(new Vector3(0f, coinForce, 0f), ForceMode.Impulse);

    goldGain.goldGain++;

    totalGold.totalGold++;

    totalGoldNew = totalGold.totalGold;

    PlayerPrefs.SetInt("TotalGoldNew", totalGoldNew);

}

private void spawnHealthPack()
{
    spawnHealthPackNumber = Random.Range(1, 101);
    if(spawnHealthPackNumber >= 99)
    {
        Instantiate(HealthPack, transform.position, transform.rotation);
    }
}

IEnumerator knockback()
{
    moveSpeed = moveSpeed * -3;
    yield return new WaitForSeconds(0.2f);
    moveSpeed = moveSpeedA.moveSpeedA;
}

}

Sounds like what you’re looking for is a LayerMask: Unity - Scripting API: LayerMask

From the docs:

public class ExampleClass : MonoBehaviour {
    public LayerMask mask = -1;
    void Update() {
        if (Physics.Raycast(transform.position, transform.forward, 100, mask.value))
            Debug.Log("Hit something");
        
    }
}

Can you post full code? I have no idea if you’ve accounted for "what happens if the raycast doesnt hit anything… is there a max range etc)

if (Physics.Raycast (rayOrigin, Cam.transform.forward, out hit, maxRange))
            {
                // hit something
                laserLine.SetPosition (1, hit.point);
            }
            else
            {
                // hit nothing and reached max range 
                laserLine.SetPosition (1, rayOrigin + (Cam.transform.forward * maxRange));
            }