- Home /
Other
Vector2.MoveTowards problem
Hi there, I am having some problems with moving an enemy towards the player when it detects that the player is near it. The code is pretty straight forward. The Update() function checks if the player is close enough to the enemy, and when it is, the boolean variable is set to true, and when it is true its supposed to move the enemy towards the player. Here is the code:
using UnityEngine;
public class Slime_Attack : MonoBehaviour
{
public bool attack;
GameObject player;
float moveSpeed;
void Start()
{
moveSpeed = 3;
player = GameObject.FindGameObjectWithTag("Player");
// attack = false;
}
void Update()
{
checkForPlayer();
if (attack)
{
transform.position = Vector2.MoveTowards(transform.position, player.transform.position, moveSpeed * Time.deltaTime);
}
}
private void checkForPlayer()
{
if (Vector2.Distance(transform.position, player.transform.position) >= 5) attack = false;
else if (Vector2.Distance(transform.position, player.transform.position) <= 3) attack = true;
}
}
The problem that I am facing is that this is kind of inconsistent. The enemy moves towards the player fine in the beginning and then it either slows down when its close to the player or in some rare cases it move really fast towards the player. It slows down especially when the player is on the left of the player for the most part, but then again in some cases, it also moves slowly when the player is to its right. I have read every documentation regarding Vector2.MoveTowards and have checked almost all other answers similar to this problem but I still cant get this to work. Any help is appreciated. Thanks
Alright guys I figured it out, basically what u have to do is, if the gameobject has a rigidbody component, then you need to use GetComponent<Rigidbody2D>().$$anonymous$$ovePosition(Vector2.$$anonymous$$oveTowards(transform.position, player.transform.position, moveSpeed * Time.deltaTime));
. This will fix the problem. Thanks :)