Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

run away ai?

Discussion in 'Scripting' started by Nyquest, Aug 14, 2010.

  1. Nyquest

    Nyquest

    Joined:
    Aug 14, 2010
    Posts:
    3
    hi

    i am making uh game where player chase object try and catch it. i have trouble with ai for object to run away.

    here is tricky part. i would like to have ai face enemy as actually it move backwards as player get closer it trys to run away. facing player is not uh problem, but how to get it to move in backwards direction?

    sorry if i not make much sense.

    here is idea overall.

    animal stalks player (working) but keeping uh distans=ce (working)

    player try to chase animal animal back up still facing player. this part not working.

    after too much, animal turn around run away to try make sure player not get too close yes? working.

    see?

    sorry again! thank you!
     
    Der_Kevin likes this.
  2. dart

    dart

    Joined:
    Jan 9, 2010
    Posts:
    211
    Find the Vector from the player to the NPC and move in that direction.

    Code (csharp):
    1.  
    2. Vector3 moveDir = npc.transform.position - player.transform.position;
    3.  
    4. transform.Translate(moveDir.normalized * speed * Time.deltaTime);
    5.  
     
  3. Zero_Quantum

    Zero_Quantum

    Joined:
    Jul 28, 2010
    Posts:
    25
    not tested but must(can) be like this:

    Code (csharp):
    1.  
    2.  
    3. float amount;
    4. public GameObject player;
    5. Vector3 direction;
    6. Vector3 runPoint;
    7. Vector3 walkPoint;
    8. float speed;
    9.  
    10. void Start(){
    11. runPoint = transform.position;
    12. walkPoint = transform.position;
    13. }
    14.  
    15. void Update(){
    16. amount = ((player.transform.position)-(transform.position)).magnitude;
    17. direction = ((player.transform.position)-(transform.position)).normalized;
    18. if(amount>100){ // if player is more then 100 units away
    19. normal(); // normal waypoint system or what you want
    20. }
    21. else if(amount>20){ // player is beetwen 20 and 100 units away
    22. chased(); // animal has chased the player
    23. }
    24. else{ // player is less then 20 unity away
    25. run();
    26. }
    27. }
    28.  
    29. void normal(){
    30. if((walkPoint-transform.position).magnitude<5){
    31. // get new point here
    32. }
    33. else{
    34. // moving script here
    35. }
    36. }
    37.  
    38. void chased(){
    39. transform.LookAt(new Vector3(player.transform.position.x,0,player.transform.position.z));
    40. transform.position+=-direction*speed*Time.deltaTime;
    41. }
    42.  
    43. void run(){
    44. if((runPoint-transform.position).magnitude<5){
    45.  
    46. }
    47. else{
    48. // moving script here
    49. }
    50. }
    51.  
    this is how i would solve this problem
    you just have to write those lines to get new point with your conditions and the walkstuff( dont know if you are doing this with RB or CC or maybe something else?

    i didnt test the code, so it may have bugs
     
  4. Nyquest

    Nyquest

    Joined:
    Aug 14, 2010
    Posts:
    3
    dart:
    thanks for answer! from look at code this moves animal toward player no? will try in uh bit but looks similar to what i use for animal to come to player.


    zero_quantum:
    thanks for answer! i think you explain how to decide if animal stalk player or run away. it is little different from how i set up, sorry i did not give code example.

    what i am looking for is actually how to reverse the direction the animal is walking so instead of going toward the player it goes awway from the player while still looking at the player.

    vector3

    my code now finds vector3 between player an animal and moves to player. i wonder how to reverse direction so animal move away from player.

    thanks again all your help! i really appreciate any response!
     
  5. Zero_Quantum

    Zero_Quantum

    Joined:
    Jul 28, 2010
    Posts:
    25
    well my script already does it:

    transform.position+=-direction*speed*Time.deltaTime;

    thats the line

    -direction or +direction depends on how u defined it.
    if your direction equals player.position - animal.position you have to take -direction

    if you give me your code i can tell you what you have to change...
     
  6. Nyquest

    Nyquest

    Joined:
    Aug 14, 2010
    Posts:
    3
    both of these work! i am sorry. thank you so much!

    darts is slightly wrong. its player.position-npc.position instead of other way. smooth movement good!

    zero work good too, except moves is not smooth but chunky.

    i think both do not have gravity and collision for move through walls. animal should move uh lon side wall.

    i use ai script for move now from fps demo shooter from unity for move forward. try to use same for move away but do not know how to get working! i should post as example someone can help maybe fix? i only wonder because if better this way ok but if zero or dart are right on uh answer i will use that!

    sorry- just read zero say give code and will fix! i will do this! have to get laptop back from brother first. thank you!
     
  7. BurningthumbStudios

    BurningthumbStudios

    Joined:
    Apr 28, 2008
    Posts:
    95
    Use the script that controlls the large birds( I think they're cranes) found in the island demo.