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);
}
}
}