# Finding enemy object in player's forward direction within a range

I would like to know the way by which I can find enemy objects tagged with "player" in a forward direction within a range say 3 units from the player.

• I am not getting any clue about the logic. That is the reason I posted. Any clue would be appreciated. – Gaurav Shekhar Aug 2 '15 at 13:56
• Do you mean within a 3 unit distance (ie. straight line), or a 3 unit wide range (ie. fanned out)? – Fuzzy Logic Aug 2 '15 at 16:11

What you need is to cast a raycast forward like this: 

     Raycast hit;
Vector3 fwd = transform.TransformDirection (Vector3.forward);
if(Physics.Raycast(transform.position, fwd, out hit, RANGE)){
if(hit.collider.tag == "Player"){
...
}


RANGE = max range eg.3

• Glad I helped. Would you mind up voting my answer and marking it as best if it worked for you – Vadim Tatarnikov Aug 3 '15 at 7:18
• I did try to vote, but I think due to lack of reputation its not letting me do so. Though I have accepted the answer. Thanks again. – Gaurav Shekhar Aug 3 '15 at 7:29