Pushing enemy makes them stop following the player
Im making a top down 2d game and im trying to make the enemy follow the player, below is my script for how i do it. Now it works fine but if the player pushes the enemy, then the enemy starts moving really slowly in just one direction until either the player leaves its range or it hits the wall, when the player leaves its range it goes flying off to the right, and if the enemy hits the wall it starts following the player again, is there a way i can fix this? (also i know the way ive set up the target isnt the best but its the only way i can get it to work.
Edit: it has something to do with the rigidbody 2D, if i remove it i can push the enemy no problems, but of course then the enemy can move throught walls, is there maybe an option i can change?
Edit 2: Problem Solved!! I changed Linear Drag to 100 and the problem is fixed
public class EnemieFollowController : MonoBehaviour {
public float moveSpeed;
Transform target;
public float minDistance;
private float range;
//public Transform target;
//public float chaseRange;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
target = GameObject.FindWithTag("Player").transform;
range = Vector2.Distance(transform.position, target.position);
if (range < minDistance)
{
//Debug.Log(range);
transform.position = Vector2.MoveTowards(transform.position, target.position, moveSpeed * Time.deltaTime);
}
}
}
Your answer
Follow this Question
Related Questions
Add a public Tranform target from another scene 0 Answers
Enemy wont deal damage 0 Answers
Unity 5.4 2D Respawn Player at start point 1 Answer
Make enemy face left or right depending on player position 1 Answer
Is there any way to trigger a popUp UI when a player steps in a specific set of tiles? 0 Answers