Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

2D enemy follow ai

Discussion in '2D' started by shemtom2002ke, Jul 29, 2021.

  1. shemtom2002ke

    shemtom2002ke

    Joined:
    Jul 5, 2021
    Posts:
    25
    I was hoping for someone to really help me here. I have an enemy who is supposed to follow my player. So I created two variables the follow distance and catchdistance. The followdistance allows the player to be able to continue running but the enemy is still following but when the player reduces the speed and the distance reduces to the catchdistance then player death/GAME OVER/enemy caught animation. (Almost similar to Subway surfers but in 2d)

    PROBLEMS
    1. The player is still movable after being caught/GameOver
    2. The enemy does not follow the distance guideline as explained (if when catchdistance is less than the followdistance then caught the player/GAMEOVER/caught animation) (else if the followdistance is greater switch to follow animation and follow the player)

    HERE is my Code

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class enemy : MonoBehaviour
    6. {
    7.     Rigidbody2D rb;
    8.     Animator anim;
    9.  
    10.     //distance when the player is caught
    11.     public float followdistance;
    12.     public float catchdistance;
    13.     public float speed;
    14.     public bool iscaught;
    15.     Player player;
    16.  
    17.  
    18.     [SerializeField]
    19.     public Transform target;
    20.  
    21.     // Start is called before the first frame update
    22.     void Start()
    23.     {
    24.         rb = GetComponent<Rigidbody2D>();
    25.         anim = GetComponent<Animator>();
    26.         player = GetComponent<Player>();
    27.  
    28.         target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    29.  
    30.         iscaught = false;
    31.     }
    32.  
    33.     // Update is called once per frame
    34.     void Update()
    35.     {
    36.         Move();
    37.         jump();
    38.     }
    39.  
    40.     public void Move()
    41.     {
    42.  
    43.         //catch the player when the player is near
    44.         if(Vector2.Distance(transform.position, target.position) > followdistance )
    45.         {
    46.             transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);    
    47.                                                        
    48.         }
    49.  
    50.         else if(catchdistance <= followdistance)
    51.         {
    52.             //gameover
    53.             iscaught = true;
    54.  
    55.             //caught anim
    56.             anim.SetBool("caught", true);
    57.  
    58.             //dont move player or enemy
    59.  
    60.         }
    61.  
    62.         else if (catchdistance >= followdistance)
    63.         {
    64.             //continue running and follwing player
    65.             anim.SetBool("caught", false);
    66.  
    67.         }
    68.  
    69.     }
    70.  
    71.     public void jump()
    72.     {
    73.        
    74.     }
    75.  
    76. }
    77.  
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,452
    So for the 1st issue, you have a isCaught bool but you need something on your player or gameController scripts to End the game/destroy the player/stop player movement/etc. when that bool becomes true.

    For the 2nd issue, throw in some debug.logs in your If and Else If blocks to see if those ever get called. You can also use those debug.logs to check to make sure the followDistance is the value you want. Im guessing that since you arent calculating the followDistance in Update is that followDistance and catchDistance are always the same as what you set in the inspector. Aka, they arent being calculated based on the enemy's and player's distance in real time.
     
  3. shemtom2002ke

    shemtom2002ke

    Joined:
    Jul 5, 2021
    Posts:
    25
  4. shemtom2002ke

    shemtom2002ke

    Joined:
    Jul 5, 2021
    Posts:
    25
    how should I be able to calculate the distance from player and enemy, real-time.
     
  5. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,452
    Im going to let you look this up, there are plenty of tutorials and forums with this.

    But to give you a head start: You will want a variable (either vector2 or float) that gets updated in well, the Update function. Essentially, distance = EnemyPos - PlayerPos or something like that. But i recommend looking it up for all the little nuances in that.
     
  6. shemtom2002ke

    shemtom2002ke

    Joined:
    Jul 5, 2021
    Posts:
    25
    i just used vector2.Distance(enemy distance, player distance).