Search Unity

  1. We are migrating the Unity Forums to Unity Discussions. On July 12, the Unity Forums will become read-only. On July 15, Unity Discussions will become read-only until July 18, when the new design and the migrated forum contents will go live. Read our full announcement for more information and let us know if you have any questions.

Question How to make smooth target following

Discussion in '2D' started by zazas321, Jun 8, 2024.

  1. zazas321

    zazas321

    Joined:
    Aug 19, 2020
    Posts:
    30
    I have been learning the basics of game development and made significant progress.

    I would like to get some advice regarding Enemy movement. In particular I would like to know how to make sure multiple enemies on the screen follow a single target (Player) without glitching/bumping into each other.

    A little bit about my configuration:
    I have a simple PlayerFollower script attached to the Enemy prefabs:


    Code (CSharp):
    1.  
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEditor.Callbacks;
    5. using UnityEngine;
    6.  
    7. public class PlayerFollower : MonoBehaviour
    8. {
    9.     //set the values in the inspector
    10.     private Transform target; //drag and stop player object in the inspector
    11.     public float within_range;
    12.     public float speed;
    13.  
    14.  
    15.  
    16.     void FixedUpdate()
    17.     {
    18.         target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
    19.         //get the distance between the player and enemy (this object)
    20.         float dist = Vector3.Distance(target.position, transform.position);
    21.         //check if it is within the range you set
    22.         if (dist <= within_range)
    23.         {
    24.             //move to target(player)
    25.             if (dist > 2.0f)
    26.             {
    27.                 transform.position = Vector3.MoveTowards(transform.position, target.transform.position, speed * Time.fixedDeltaTime);
    28.             }
    29.             else{
    30.                 //stop moving
    31.                 transform.position = this.transform.position;
    32.             }
    33.         }
    34.     }
    35.  
    36. }
    37.  
    As you can see from above, its quite simple and self explanatory. The enemy is trying to reach the position of the Player. If the distance is < 2 then the enemy will stop moving.

    Both the Enemy and Player have the same settings:
    RB Body type: Dynamic
    Mass: 1
    Collision Detection : Continuous
    Layer: Default
    Sorting Layer: Default
    Order in Layer 0





    I would like to know how to ensure the enemies stop glitching on top of each other without adding significant complexity to the project. Does it have to do with the Dynamic body type and mass of the enemy or the player? Does it have to do with the layers the enemy/player are in? Does it have to do anything with the colliders?

    Appreciate any tips in advance.


    For a quick demonstration I have made a GIF:
    https://ezgif.com/video-to-gif/ezgif-4-c208ff6a01.mp4
     
  2. dstears

    dstears

    Joined:
    Sep 6, 2021
    Posts:
    241
    If you want Unity physics collisions to work, then you should be using RigidBody2D.MovePosition rather than changing the transform.position directly. When you change the transform.position, the object just teleports to that location with no collision checking.

    https://docs.unity3d.com/ScriptReference/Rigidbody2D.MovePosition.html

    I think this will stop your enemies from overlapping, but they are still going to bump into each other if they are both trying to move to occupy the same space.