- Home /
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?
What I visualize is a head on a parent that can look up or down but cannot turn side to side...or a gun on a turret that can look up or down but it is the turret that rotates. Is this correct? And is the child trying to get the elevation of the world target? And is the parent 'y' axis aligned or can it have arbitrary rotation?
Not really. The parents down vector always points to the center of the planet(its Y-Rotation doesn't matter). The child should inherit the rotation but can freely look left/right (Y).
The problem is if I use transform.LookAt(new Vector3(forwardObject.position.x, transform.localPosition.y, forwardObject.position.z)
the X and Z rotation of the child are not inherits the parents rotation - it is just locked at the world space.
So the childs down vector should always point to the planets center, but turns at a target object but only uses the Y axis.
Sorry - it's kind of hard to explain for me.
So just to be clear if the parent is a body walking around on a planet. The head can look left and right with respect to the body AND, given the head restrictions, the head tries to track some target?
yepp: head.eulerAngle.x = 0 in local space (body) head.eulerAngle.y = tries to look at the target. head.eulerAngle.z = 0 in local space (body)
I really really really needed this and after a few days of searching I found this answer. Thank you. So. $$anonymous$$uch.
I love you. (>^-^)>
Answer by robertbu · Oct 30, 2013 at 03:24 PM
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);
Thank you!! To be honest I have not idea how Vector3.Dot works... but it works! I'll try to understand it later :-)
$$anonymous$$aybe interesting for others: In my case I needed to put that code into a LateUpdate function. Otherwise it was jittering.
While understanding Vector3.Dot() works is nice to know, the top two lines of code just project a point onto a plane. As a way of getting some of the functionality before having a deep understanding of Vector math, take a look at the Wiki $$anonymous$$ath3d script. It gives you lots of functions for 3D geometry that you can use including ProjectPointOnPlane().
just thaught id post a C# version of the above code as i found it very usefull.
float DistanceToPlane = Vector3.Dot(body.up, target - head.position);
Vector3 plantPoint = target - head.up * DistanceToPlane;
firePoint.transform.LookAt(plantPoint, body.up);
I am trying to get similar results, but my lookAt is looking at a raycast hit.point. I can get the "head " to turn around on it's local axis but once the raycast hit.point moves the opposite side of the original hit, the "head" jitters and jumps around. What I want is to have the head look at the hit.point and swivel on it's local y axis. I am not sure why it is jumping around when the hit.point moves... can anyone explain this to me?
And bonus points if it can do the Quaternion.Slerp
Your answer
Follow this Question
Related Questions
Look at around one axis in local space 1 Answer
i want to know what object is the closer to the camera to see 1 Answer
[RePost for No Answer] How to make a character look at the direction it's going towards 1 Answer
replace raycast with transform direction 1 Answer
Indicator rotating around player 1 Answer