Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

AI that follows the player if he is in range ("answered")

Discussion in '2D' started by tiancar, Mar 6, 2018.

  1. tiancar

    tiancar

    Joined:
    Jun 17, 2014
    Posts:
    5
    Hi, I need a little bit of help with this AI in my 2D game. It is supposed to follow the player when the player comes within the range (float specified in the editor). I've searched the forum and none seem to work for me. Also why do I have to increase my range to like 100 before something happens even if they are just 1 apart. Just for info smart enemy is a bool I set in the editor.


    Code (CSharp):
    1.  Target = new Vector2(Player.transform.position.x, Player.transform.position.y);
    2.         Distance = Vector2.Distance(transform.position, Target);
    3.  
    4.         if (Distance <= DetectRange && SmartEnemy == true)
    5.         {
    6.             InRange = true;
    7.             Vector2 velocity = new Vector2((transform.position.x - Player.transform.position.x) * MoveSpeed, (transform.position.y - Player.transform.position.y) * MoveSpeed);
    8.             gameObject.GetComponent<Rigidbody2D>().velocity = -velocity/100; // I divided by 100 or it flies of the screen
    9.         }
    10.         else
    11.         {
    12.             InRange = false;
    13.            //here is an code for what to do when it does
    14.            not 'see' the player. it works so i cut it out here and it is
    15.            not the problem
    16.          }
    17.  
    18.  

    Thanks, Tian
     
  2. tiancar

    tiancar

    Joined:
    Jun 17, 2014
    Posts:
    5
    If it helps the enemys velocity when it does not 'see' me is a vector2 with values between -1f and 1f and is multiplied by MoveSpeed.
     
    Last edited: Mar 6, 2018
  3. tiancar

    tiancar

    Joined:
    Jun 17, 2014
    Posts:
    5
    Why is my post awaiting moderation?
     
  4. JoeStrout

    JoeStrout

    Joined:
    Jan 14, 2011
    Posts:
    9,840
    You're setting the velocity proportional to how far away the target is. I don't see how that makes sense (but maybe you do?).

    Apart from that, what does this do that you don't want it to, or fail to do that you want it to do?
     
  5. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    You can try something like this..
    Code (csharp):
    1. public Transform player; // changed this to Transform
    2. public float detectRange = 10; // this gets multiplied by itself to compare to a sqr magnitude check (instead of distance)
    3. public bool inRange = false;
    4. public float moveSpeed = 2f; // you can adjust this, of course.
    5. Rigidbody2D rb;  // cached the reference, so you can avoid GetComponent calls in Update/FixedUpdate.
    6.  
    7. void Awake()
    8. {
    9.    rb = GetComponent<Rigidbody2D>();
    10.    detectRange *= detectRange;
    11. }
    12.  
    13. void Update()
    14. {
    15.    // a little cheaper than 'distance'.. deleted the code to create a position from the player values.
    16.    float distsqr = (player.position - transform.position).sqrMagnitude;
    17.  
    18.    if (distsqr <= detectRange)
    19.    {
    20.       inRange = true;
    21.     // get a velocity based on the normalized direction, multiplied by move speed.
    22.       Vector2 velocity = (player.transform.position - transform.position).normalized * moveSpeed;
    23.       rb.velocity = velocity;
    24.    }
    25. }
    Let me know if that works okay, and/or if you have any questions.. :)

    Oh, right I deleted the SmartEnemy variable, but it didn't seem important for my test. ;)
    You can put it back in..
     
  6. tiancar

    tiancar

    Joined:
    Jun 17, 2014
    Posts:
    5
    Figured it out by myself, because for some reason my post was awaiting moderation, which ment no quick answers or clues. One of the answers to the question similar to mine worked for me, but didnt at first because I was stupid and dragged the Player prefab instead of the GameObject, so it kept trying to go to 0,0.

    In the end I used move towards although my script "worked" (it moved slower the closer it came)
    transform.position = Vector2.MoveTowards(transform.position, Target, MoveSpeed * Time.deltaTime/1.5f);

    Thanks for the response!
     
    Last edited: Mar 7, 2018
    JoeStrout likes this.
  7. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Cool, glad you got it sorted out. :)
    Just a quick thought, but if you are adding " / 1.5f" you may as well just divide MoveSpeed by 1.5 , but so move speed is set to that answer, rather than a calculation..
     
    murco likes this.
  8. Deleted User

    Deleted User

    Guest

    Can you make this code for 3d please and with rotation for the enemy to face the player?
     
  9. henrydavidsmith2010

    henrydavidsmith2010

    Joined:
    Feb 20, 2021
    Posts:
    1
    1. public Transform player; // changed this to Transform
    2. public float detectRange = 10; // this gets multiplied by itself to compare to a sqr magnitude check (instead of distance)
    3. public bool inRange = false;
    4. public float moveSpeed = 2f; // you can adjust this, of course.
    5. Rigidbody3D rb; // cached the reference, so you can avoid GetComponent calls in Update/FixedUpdate.

    6. void Awake()
    7. {
    8. rb = GetComponent<Rigidbody2D>();
    9. detectRange *= detectRange;
    10. }

    11. void Update()
    12. {
    13. // a little cheaper than 'distance'.. deleted the code to create a position from the player values.
    14. float distsqr = (player.position - transform.position).sqrMagnitude;

    15. if (distsqr <= detectRange)
    16. {
    17. inRange = true;
    18. // get a velocity based on the normalized direction, multiplied by move speed.
    19. Vector3 velocity = (player.transform.position - transform.position).normalized * moveSpeed;
    20. rb.velocity = velocity;
    21. }
    22. }



    some thing like this for 3d
     
  10. murco

    murco

    Joined:
    Nov 17, 2021
    Posts:
    1
    thank you so much!!!!!!