LookAt locked at Y-axis but in local space

Hi!

I used the Faux Gravity Script to work in planetary Setup…
However I have a gameobject that is always looking at the worlds center. I put another gameobject into it. This child should look in a special direction but must be locked on the local Y axis. Using

transform.LookAt(new Vector3(forwardObject.position.x, transform.localPosition.y, forwardObject.position.z)

makes the child looking at the target and it is looked a the Y axis, but the X and Z axis are looked at the world space. I need them to be looked at the localspace, so that the child is always aligning at its parent. Hope this is understandable.

Do someone have a solution for that?

I’m going to use head and body here as the transform for the head and the body, where the body is standing on the planet and the head can look left and right with respect to the body. The easiest way to make this work is to project the target point onto a plane that has a normal of the body’s up.

var distanceToPlane = Vector3.Dot(body.up, target.position - head.position);
var planePoint = target.position - body.up * distanceToPlane;
head.LookAt(planePoint, body.up);