I've got a problem with enemies following the player.
Hello! I am making a small 2D-platform type game where enemies spawn at random locations and attempt to kill the player. The problems basically are:
Even though I have set the speed to be extremely slow (for testing reasons), the enemies still move towards the player in a stable and sort of fast speed.
The enemies don't keep a distance from the player (even if I set it to be extremely big for testing reasons again) and tend to get "inside" the player sprite. I am new to Unity coding and, after searching forums, I have yet to find an answer. Thanks for your time! Please excuse me for my weird coding ;P
Here's my current code:
 public float moveSpeed = 0.005f; // the moving speed of the enemies
 public float stopDistance = 500000f; // the distance from which enemies stop following the player
 private Transform target; // the target is basically the player
 Vector2 targetPos; // the position the enemy is supposed to move towards
 private void Start()
 {
     currentHealth = maxHealth; // ignore this part, it's not for movement
     target = GameObject.Find("LightPlayer").GetComponent<Transform>();
 }
 private void FixedUpdate()
 {
     if (transform.position.x < target.position.x)
     {
         targetPos = new Vector2(target.position.x - stopDistance, transform.position.y);
     }
     else if (transform.position.x > target.position.x)
     {
         targetPos = new Vector2(target.position.x + stopDistance, transform.position.y);
     }
     transform.position = Vector2.MoveTowards(transform.position, targetPos, moveSpeed * Time.deltaTime);
 }
 
              Your answer
 
             Follow this Question
Related Questions
InvalidOperationException thrown 0 Answers
help with player movement 0 Answers
Bullet spawning at high Player speed 1 Answer