0
\$\begingroup\$

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.

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

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

\$\endgroup\$
  • \$\begingroup\$ Glad I helped. Would you mind up voting my answer and marking it as best if it worked for you \$\endgroup\$ – Vadim Tatarnikov Aug 3 '15 at 7:18
  • \$\begingroup\$ 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. \$\endgroup\$ – Gaurav Shekhar Aug 3 '15 at 7:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.