Question by
adrianobgtt · Oct 18, 2015 at 06:54 PM ·
c#enemyfollow player
How to make the enemy follow 2 players
Hi, I'm making a 3d minigame where I want to make the enemy follow two players, I have a spawn and the two players, but the enemy focus in one and dont change, what I want is that the enemy changes the focus when the other player is nearest than the other. Thank you!
Comment
Answer by hvd · Dec 03, 2015 at 11:18 PM
Try to find the position of your players like that
Player1 = GameObject.FindGameObjectWithTag("Player1").transform;
...
then get the range of your enemy to the players
rangeplayer1 = Vector3.Distance(transform.position, Player1.position);
...
and then just look what distance is closer
if(rangeplayer1<rangeplayer2)
{transform.position=Vector3.Lerp(transform.position, Player1.position ...}
...
Your answer
![](https://koobas.hobune.stream/wayback/20220612041820im_/https://answers.unity.com/themes/thub/images/avi.jpg)