Hi I am trying to make it so enemies move toward the player in 2d. I already made them able to move but I do not know how to make it so they turn around to face me. Heres what I have so far:
private GameObject Player;
public Transform target;
public float speed = 3f;
void Start()
{
Player = GameObject.Find("Player"); // target
}
void Update()
{
//move towards player
if (Vector3.Distance(transform.position, target.position) > 1f)
{
transform.position = Vector2.MoveTowards(transform.position, Player.transform.position, speed * Time.deltaTime);
}
}

Here’s the script:
using UnityEngine;
public class Enemy : MonoBehaviour
{
public Transform target;
public float speed = 3f;
private void Update()
{
if (Vector3.Distance(transform.position, target.position) > 1f)
{
MoveTowardsTarget();
RotateTowardsTarget();
}
}
private void MoveTowardsTarget()
{
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
private void RotateTowardsTarget()
{
var offset = 90f;
Vector2 direction = target.position - transform.position;
direction.Normalize();
float angle = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.Euler(Vector3.forward * (angle + offset));
}
}
Good day.
Did you tried transform.LookAt() ?
I want you to know sir, that you, yes you, are a god among men. I spent HOURS trying to make a fireball rotate in the direction of its path. You saved my night.