Unity Community


Results 1 to 7 of 7

Thread: run away ai?


  1. Posts
    3

    run away ai?

    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. Location
    Brasil
    Posts
    207
    Find the Vector from the player to the NPC and move in that direction.

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


  3. Posts
    25
    not tested but must(can) be like this:

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

    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. 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. Posts
    25
    Quote Originally Posted by Nyquest
    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 away 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!
    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. 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. Location
    Canada
    Posts
    49
    Use the script that controlls the large birds( I think they're cranes) found in the island demo.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •