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.
\$\begingroup\$
\$\endgroup\$
2
-
\$\begingroup\$ I am not getting any clue about the logic. That is the reason I posted. Any clue would be appreciated. \$\endgroup\$Gaurav Shekhar– Gaurav Shekhar2015-08-02 13:56:24 +00:00Commented Aug 2, 2015 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– Fuzzy Logic2015-08-02 16:11:30 +00:00Commented Aug 2, 2015 at 16:11
Add a comment
|
1 Answer
\$\begingroup\$
\$\endgroup\$
2
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
-
\$\begingroup\$ Glad I helped. Would you mind up voting my answer and marking it as best if it worked for you \$\endgroup\$John Smith– John Smith2015-08-03 07:18:17 +00:00Commented Aug 3, 2015 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– Gaurav Shekhar2015-08-03 07:29:41 +00:00Commented Aug 3, 2015 at 7:29