Search Unity

How to make Ai move back from player?

Discussion in 'Scripting' started by wander_dev, May 19, 2017.

  1. wander_dev

    wander_dev

    Joined:
    Sep 29, 2014
    Posts:
    18
    Most tutorials only show you how to move towards the player, and that's great and all. But i would love to know how to move my AI back to a certain position once its reached its destination and start attacking again. I've been working on this for weeks i can't figure it out

    Code (CSharp):
    1.     if (playerDistance < 500f )
    2.         {
    3.  
    4.             if (playerDistance > 5f)
    5.             {
    6.  
    7.                 // prevents enemy from getting to close to player
    8.                
    9.                 Chase();
    10.                 transform.LookAt(player);
    11.                 Attack();
    12.  
    13.  
    14.  
    15.             }
    16.  
    17.         }
    18.  

    Code (CSharp):
    1.   void Chase()
    2.     {
    3.         animator.SetFloat("Vertical", 1);
    4.  
    5.         transform.Translate(Vector3.forward * moveSpeed * Time.deltaTime);
    6.  
    7.     }
    Code (CSharp):
    1.  void BackAway()
    2.     {
    3.          transform.Translate(Vector3.back * moveSpeed * Time.deltaTime);
    4.  
    5.  
    6.     }
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Depending on your situation, maybe the enemy wants a range for attacking? If they're too close, you could:
    Code (csharp):
    1.  transform.Translate(-transform.forward * moveSpeed * Time.deltaTime);
    otherwise, they're either approaching (if too far), or attacking if within range?
     
  3. wander_dev

    wander_dev

    Joined:
    Sep 29, 2014
    Posts:
    18
    Hey! Thanks for the reply. I changed the BackAway() method to your code. It repeats both Chase(); and BackAway(); is their a better way to do this?

    Code (CSharp):
    1.   if (playerDistance < 5f)
    2.             {
    3.                 BackAway();
    4.             }
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, I think if you're using > 5 and < 5 it will be "stuck" sort of, right, because it's always going "close, away, close, away, etc.."? Is that what you're saying?
     
  5. wander_dev

    wander_dev

    Joined:
    Sep 29, 2014
    Posts:
    18
    Haha yeah. thats what i was trying to say. You explained it a lot better :). Is their a way to say. Enemy move closer , stop , then move back , stop.

    As it is now it just moves closer. and i can't move back. Also Thanks a bunch for taking the time to help me. I'm not that smart.
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Well, don't give up on yourself too soon. Just takes some practice to think about things and how they work (or might work lol).
    Okay, well, I would imagine that there is a certain range in which the enemy can attack ,right?
    So, maybe once it gets that close, it starts attacking.. then if the player is too far, it goes closer.
    If the player gets closer, though, the enemy doesn't have to move right away, but rather it moves if the player is "too close".
    So, it might look something like:
    Range is 3- 8 (just for an example)
    approach player while farther than 8 (or whatever number).
    At any time, should the player be closer than 3, move away. Farther than 8, approach.

    You could tweak that a little to try to get the enemy to "stop" in the middle (maybe 5 or 5.5, that way they're not always on the edge.. but they can start attacking before they're done moving - unless that's not allowed).

    haha.. All depends on how you go about it. For starters though, I'd say just do the range thing:
    > 5 approach.
    < 2 backaway
    in between, attack. :)
     
    wander_dev likes this.
  7. wander_dev

    wander_dev

    Joined:
    Sep 29, 2014
    Posts:
    18
    Wow that sound great i'll go try to code it. The move father away might be difficult i'll give it a go. Thanks again
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    When you say the moving farther away is tough, how so? Do you mean like the enemy might walk into a wall or off of a cliff? lol I'm not very familiar with path/pathfinding stuff, and not even sure if that's what you meant.

    As for generally just "moving backwards", I think the code I wrote above should work.
    transform.forward is a forward based on object's position, whereas (in some other code you wrote) Vector3.forward and the like, are world 'forward'/'/backward'/'right' etc..

    If there's some other complication not mentioned, come back n explain it after you code the little bit about staying in the range and maybe I / someone else can help you sort that out :)
     
  9. wander_dev

    wander_dev

    Joined:
    Sep 29, 2014
    Posts:
    18
    Sorry i have to get better at clarifying. In the example Range is 3- 8 when the enemy AI is less than 3 move beyond 8. But if the enemy Ai starts move forward at 8 how to move beyond the 8 range
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Not sure what you mean by move beyond the 8 range.
    Let me rephrase just in case something could have been better written:

    From the enemy's perspective:

    Slightly expanding on my previous answer...
    if chasing and player > 5, approach.
    if chasing and player <= 8 attack (+ continue approach)
    if chasing and player < 5 , no longer be in "chase" mode. Just attacking
    if player < 3 backaway until 5 or 8
    if player > 8 in attack mode, attack is stopped and chase (resumes).

    This could be simpler to have a trial run, though.

    If player > 8 chase.
    else if player <= 8 and >= 3 attack
    else if player < 3 MoveBack.
     
    wander_dev likes this.
  11. wander_dev

    wander_dev

    Joined:
    Sep 29, 2014
    Posts:
    18
    oh OK i see now thanks i'll give it a go
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Sure, let me know how it goes :)
     
  13. wander_dev

    wander_dev

    Joined:
    Sep 29, 2014
    Posts:
    18
    i completed the script today, i even added a timer. Thanks so much for taking the time to help me.
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    That's great. :) You're welcome. Enjoy your game.