Hello, I have this script that is supposed to be attached to an enemy. Then, the enemy must look at the player and walk onto him. The problem is that the enemy is looking at the player, but he constantly goes up, and not towards the player. Any help??
function Update() {
//Pl is the variable for player gameobject.
var dir = pl.transform.position - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg - 90;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);
//walk to player
rigidbody2D.velocity = new Vector2(0, Mathf.Lerp(0, speed, 0.8f));
}
EDIT: I found a solution:
rigidbody2D.velocity = transform.TransformDirection(Vector2(0,Mathf.Lerp(0, speed, 0.8f)));