- Home /
raycast to select transform but not sphere collider
Hi everyone,
Probably very simple but I'm new and would appreciate any help given :). Scenario: So I'm currently trying to left click to select enemies and have them appear as the player target, which is so far successful.
However - My enemy prefabs have to have a Sphere Collider with "is trigger" enabled (currently set at radius 10) attached to them so that my aggro radius works e.g. OnTriggerEnter(Collider other), but when I'm using my code below the raycast obviously hits the sphere collider (with radius 10) and selects the enemy, but as you can imagine i dont want it to select the enemy's sphere collider i want it to select just when i click on the actual model.
Heres my code (C#)
public Transform selectedTarget;
private RaycastHit hit;
void Update ()
{
if (Input.GetMouseButtonDown(0)) // if left mouse clicked
{
var ray = Camera.main.ScreenPointToRay(Input.mousePosition); // make a raycast to clicked position
if (Physics.Raycast(ray, out hit, Mathf.Infinity)) // if the raycast hit something
{
if (hit.collider.transform.tag == "Enemy") // if the raycast hit a gameobject with tag enemy
{
//target stuff
Debug.Log("targetted:" + hit.collider.transform.tag);
selectedTarget = hit.transform; // set the players target to the collided transform
}
}
}
}
Many thanks, James.
Your answer
Follow this Question
Related Questions
NPC sight by collider 0 Answers
Raycast Targeting issue 1 Answer
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Trying to get my AI to turn and face direction they are going 1 Answer