run away ai?

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

  1. Nyquest

    Nyquest

    New Member

    Joined:
    Aug 14, 2010
    Messages:
    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!
  2. dart

    dart

    Member

    Joined:
    Jan 9, 2010
    Messages:
    208
  3. Zero_Quantum

    Zero_Quantum

    New Member

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

    Code (csharp):
    1.  
    2.  
    3. float amount;
    4. Vector3 runPoint;
    5. Vector3 walkPoint;
    6. float speed;
    7.  
    8. void Start(){
    9. runPoint = transform.position;
    10. walkPoint = transform.position;
    11. }
    12.  
    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. }
    26.  
    27. void normal(){
    28. if((walkPoint-transform.position).magnitude<5){
    29. // get new point here
    30. }
    31. else{
    32. // moving script here
    33. }
    34. }
    35.  
    36. void chased(){
    37. transform.LookAt(new Vector3(player.transform.position.x,0,player.transform.position.z));
    38. }
    39.  
    40. void run(){
    41. if((runPoint-transform.position).magnitude<5){
    42.  
    43. }
    44. else{
    45. // moving script here
    46. }
    47. }
    48.  
    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

    New Member

    Joined:
    Aug 14, 2010
    Messages:
    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

    New Member

    Joined:
    Jul 28, 2010
    Messages:
    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

    New Member

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

    BurningThumb

    Member

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