How do I make an object look at an another object in Top-Down 2D? ( C# )

I’ve tried googling it but none seemed to work for me.

How can I make an object follow and LOOK AT an another object while they both move?

LookAt doesn’t work properly in 2D.

Thanks! :slight_smile:

/////

Here is my current code to follow the object.
It doesn’t contain any code to look at the object.

public Vector3 objectPosition;
private Vector3 diff;

public static float speed = 5.0f;

// Use this for initialization
void Start () {
	
	transform.position = new Vector3 (0,25,0);
	
}

// Update is called once per frame
void Update () {

	Vector3 playerPosition = GameObject.FindWithTag("object").transform.position;
	
	transform.position = Vector3.Lerp (transform.position, objectPosition, Time.deltaTime * speed);
	
}

Assuming standard 2D on the XY plane:

var dir = target.position - transform.position;
var angle = Mathf.Atan2(dir.y, dir.x) * Mathf.Rad2Deg;
transform.rotation = Quaternion.AngleAxis(angle, Vector3.forward);

This code assumes that the front side of the object is to the right. If your front side is the top, then add 90 to ‘angle’ before passing it to AngleAxis().