Achieving something like LookAt but with the X axis.

I’m working on a 2D scene and have been trying to come up with a way to get the results of look at but on the X axis, that is my current objects right faces the target. I found a way around it by using parent objects but it’s a jumbled mess. It gives me problems when I use colliders2D and mainly with sprites as these can’t be seen if you rotate the objects. So I am wondering if some of you have had this problem before and if maybe you can lend me a hand.

Calculate the direction between current Object and Target object e.g

Vector3 dir = (CurrentObject’s Position - TargetObject position). normalized;

CurrentObject.transform.right = dir;

Since this is 2D and the right side of your object is consider the ‘forward’ side to be pointed at the target, you can do it this way:

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

Might need more info but I think what you need is something like:

//playerLookVector can be where you want to look

//playerTransform is whatever object you want to look at the playerLookVector

//lookObject is the object you want to lookAT


//this.PlayerTransform.position.y will cause the player too look straight forward

playerLookVector = new Vector3 (lookObject.transform.position.x, 
											this.playerTransform.position.y, lookObject.transform.position.z);

this.playerTransform.LookAt(playerLookVector);

That’s a 3d example but pretty sure you can still use it or do something similar with Vector2 instead of Vector3.