Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

NavMesh flee. Ai flee from player.

Discussion in 'Scripting' started by Marks981, Jan 3, 2015.

  1. Marks981

    Marks981

    Joined:
    May 12, 2013
    Posts:
    4
    This script makes ai fallow player :
    Code (JavaScript):
    1.     var player: Transform;
    2.  
    3. function Start(){
    4.  
    5.     player = GameObject.FindWithTag("Player").transform;
    6.  
    7. }
    8. function Update() {
    9.  
    10.  
    11.     GetComponent(NavMeshAgent).destination = player.position;
    12.  
    13. }
    14.    

    Now,how can i make AI RUN AWAY From Player. Just Run away from player with navmesh. I know i have to get player vector and then i have to make an Ai to move there. Or maybe there is an other option?
    Help me out smart people!
     
    Last edited: Jan 3, 2015
  2. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    Pick a point where you want the AI to run to and make that the NavMesh target - you choose the hueristic to decide where the run away point is....

    e.g. 10*player.transform.forward
     
  3. Marks981

    Marks981

    Joined:
    May 12, 2013
    Posts:
    4
    No,i dont need a specific point,i need ai to run away from player without points,it is possible if player vector is found and then somehow make ai run to that player vector point,so there is no specific NavMesh targets,like waypoints or anything....
    Thanks for reply anyway :)
     
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    Yes, you do need a specific point. You need to feed the agent a target position.

    Just subtract the enemy.position from the player.position to get a direction and multiply the normal of that and add the enemy.position to get a point away from the player/
     
    Glimma likes this.
  5. CodeMonke234

    CodeMonke234

    Joined:
    Oct 13, 2010
    Posts:
    181
    You can select a random point away from the player and/or change the target frequently.

    also - you could disable navmesh and programmatically move every tick in a direction away from player
     
    Marks981 likes this.
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    If you wanted you could use a modified Dijkstra algorithm to find the optimal path away from the player. You could build this to take into account distance, cover, terrain and so forth.
     
    Marks981 likes this.