Search Unity

Keep enemies at a fixed distance

Discussion in 'Navigation' started by Elessar96, Apr 5, 2021.

  1. Elessar96

    Elessar96

    Joined:
    Jan 1, 2021
    Posts:
    2
    Hi everyone, I need a hand to solve a problem in my game. As the title suggests, the enemies must necessarily remain at a certain distance from the player. The chasing function already works, but when I go towards the enemy, the latter does not move away. The game is 3D and uses navmesh and navmesh agents for enemies. Thanks so much
     
  2. Olmi

    Olmi

    Joined:
    Nov 29, 2012
    Posts:
    1,553
    Hi,

    Here's one solution on high level - In your enemies logic check the distance to the player. If the player is within certain distance, make the enemy try to find a new target where it will move to. I don't know anything about your game or how you want the enemies to behave, but you could just calculate a vector from the player to the the enemy, and then extend the vector so that it is at least outside the perimeter you want to retain. Then check if this point is a valid navmesh location where your enemy can move and make the enemy move there. Also implement some mechanism to find an alternative location for the enemy or otherwise handle the situation.