Making an object face another

I’ll try and explain this as best I can!

I have an objectA, which orbits around another objectB. ObjectB is larger in scale on the Y axis, therefore it’s transform is higher than objectA.

I’m using transform.lookat so objectA always faces the objectB. However due to the difference in scale, the transform of objectB is higher, therefore objectA also rotates around it’s x axis to ‘look up’ at objectB transform - this is what I don’t want.

What I’m trying to accomplish, is for object A to face object B as it orbits, but with the rotation only on the Y axis so it continually faces object B, but doesnt ‘look up’.

Any advice on how to achieve this? Thanks guys.

Don’t look directly at the other object, but build a new Vector using components of the other object’s transform and the transform of the object itself.

public GameObject objectB;

private Vector3 lookAtTarget;

void Start() {
    lookAtTarget = objectB.transform.position;
}

void Update() {
    transform.LookAt(new Vector3(lookAtTarget.x, transform.position.y, lookAtTarget.z));
}

For purposes of flexibility I’d put an empty gameobject childed to Object B as a target of the lookAt.