1. Unity 5.6 is now released.
    Dismiss Notice
  2. We've introduced thread tags, search within a thread and similar thread search. Read more here.
    Dismiss Notice
  3. Learn how you'll soon be able to publish your games to China in four simple steps with Xiaomi. Sign up now for early access.
    Dismiss Notice
  4. Get further faster with the Unity Plus Accelerator Pack, free for new Unity Plus subscribers for a limited time. Click here for more details.
    Dismiss Notice
  5. The launcher is currently pulling earlier versions of the content packages which may have errors, see post.
    Dismiss Notice
  6. We've released our first Timeline Experimental Preview, our new tool for creating cutscenes and more! To check it out click here.
    Dismiss Notice
  7. Check out all the fixes for 5.5 in patch releases 1 & 2.
    Dismiss Notice

run away ai?

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

  1. Nyquest


    Aug 14, 2010

    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.


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


    Jan 9, 2010
  3. Zero_Quantum


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

    Code (csharp):
    3. float amount;
    4. Vector3 runPoint;
    5. Vector3 walkPoint;
    6. float speed;
    8. void Start(){
    9. runPoint = transform.position;
    10. walkPoint = transform.position;
    11. }
    13. void Update(){
    14. amount = ((player.transform.position)-(transform.position)).magnitude;
    15. direction = ((player.transform.position)-(transform.position)).normalized;
    16. if(amount>100){ // if player is more then 100 units away
    17. normal(); // normal waypoint system or what you want
    18. }
    19. else if(amount>20){ // player is beetwen 20 and 100 units away
    20. chased(); // animal has chased the player
    21. }
    22. else{ // player is less then 20 unity away
    23. run();
    24. }
    25. }
    27. void normal(){
    28. if((walkPoint-transform.position).magnitude<5){
    29. // get new point here
    30. }
    31. else{
    32. // moving script here
    33. }
    34. }
    36. void chased(){
    37. transform.LookAt(new Vector3(player.transform.position.x,0,player.transform.position.z));
    38. }
    40. void run(){
    41. if((runPoint-transform.position).magnitude<5){
    43. }
    44. else{
    45. // moving script here
    46. }
    47. }
    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


    Aug 14, 2010
    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.

    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.


    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


    Jul 28, 2010
    well my script already does it:


    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


    Aug 14, 2010
    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. BurningThumb


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