Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, Enemy

Hello!

I trying to make only objects that has layer “Enemy” to be raycasted by the mouse to add targets to aim at.
Everything seams to work fine except from that all objects with a collider gets raycasted even if they dont have layer “Enemy”

What have i done wrong?

CODE

namespace TurretDemo
{
   public class TurretTester : MonoBehaviour
   {
        public TurretRotation[] turret;
        public Vector3 targetPos;
        public Transform targetTransform;
        public InputField enemyName;

        [Space]
        public bool turretsIdle = false;

        [SerializeField]
        private LayerMask Enemy;

        public void Shoot()
        {
            RaycastHit hit;
            if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit,Enemy))
            {
                Debug.Log(hit.transform.name);
                targetTransform = hit.transform;
                enemyName.text = hit.transform.name;
            }
        }
   
    private void Update()
        {

            if (Input.GetMouseButtonDown(0))
            {
                Shoot();
            }
            // Toggle turret idle.
            if (Input.GetKeyDown(KeyCode.E))
                turretsIdle = !turretsIdle;

            // When a transform is assigned, pass that to the turret. If not,
            // just pass in whatever this is looking at.
            targetPos = transform.TransformPoint(Vector3.forward * 200.0f);
            foreach (TurretRotation tur in turret)
            {
                if (targetTransform == null)
                tur.SetAimpoint(targetPos);
                else
                tur.SetAimpoint(targetTransform.position);

                tur.SetIdle(turretsIdle);
            }
         }

            public void IdleWeapons()
            {
                Input.GetKeyDown(KeyCode.E);
            }

            public void EnemyClicked()
            {
                Debug.Log("Hello:");
            }

            private void OnDrawGizmos()
            {
                Gizmos.DrawWireSphere(targetPos, 1.0f);
            }
        }
    }

Can you show a screenshot of the inspector (and what the “Enemy” LayerMask looks like there)?

Enemy is just the declaration of the LayerMask - you’re not assigning any value to it. Make sure in your inspector you’re actually assigning the “Enemy” layer or you can do

private LayerMask Enemy = LayerMask.GetMask("Enemy");

to assign it directly in code.

Per here:

Thats just a list of layers that you can “check” for what you are able to click on.

With the layermask you can shose what layer you are able to raycast whith the mouse pointer as i guess you know, so what ever layer i “check” in the list should only be raycasted.
But it does raycast every collider even if i have checked “Enemy” in the inspector and given the enemy object with a collider the layer “enemy”.

Yes, I know, I’m asking for a screenshot of yours to confirm that its value is properly set up.

Everthing is fine in the inspector, and the ship you see has “enemy” as layer
http://imgbox.com/CEs17EKL
Looks like i cant link images so it shows in the forums -_-