Hello. When using transform.LookAt(Transform); is there a way to have the object only track the other object’s x and z positions and not its y(the object that I want to follow another keeps rolling over)? I have tried adding a rigidbody and constraining its rotation, but that does not work. Thanks
Adding;
transform.localEulerAngles.x = 0.0;
transform.localEulerAngles.z = 0.0;
at the end might work.
I’d suggest:
var targetPosition = target.position;
targetPosition.y = transform.position.y;
transform.LookAt(targetPosition);
Or something similar.
I use Jesse’s example in my projects. I believe localEulerAngles wouldn’t work correctly if transform was parented to another object.
maybe the ConfigurableJoint helps here.
edit: link: Unity - Scripting API: ConfigurableJoint
Try something like this:
Haven’t tested (and obviously some stuff will need to be changed in accordance to who is targeting what), but that should work so long as you update position every time you move whatever it is you are moving.
EDIT: Actually, the y position will need to be something other than 0… I dunno… maybe this could be a start to something
Your example basically looks at a point directly above, below, or coincident with the object’s position (the latter of which is likely to cause an error), which probably isn’t what the OP is looking for.
In any case, see my post above (it includes a solution to the problem).
Jesse’s solution is the most common one. Except you’d probably want “transform.position.y” on that second line there, not “transform.y”.
Yup, thanks - fixed