- Home /
LookAt To Only Rotate on Y Axis - How?
Hello,
I have a LookAt function on my turret, and right now it works fine, the turret rotates itself to look at its target, but how would I only allow my turret to rotate on the Y axis, right now it also tilts too.
This is my lookat function:
var rotation = Quaternion.LookRotation(target.position - transform.position);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
Taken from the SmoothLookAt script (assets)
Thanks.
Answer by Mike 3 · Dec 01, 2010 at 07:34 PM
You just need to make sure you're giving planar positions, i.e.:
var lookPos = target.position - transform.position;
lookPos.y = 0;
var rotation = Quaternion.LookRotation(lookPos);
transform.rotation = Quaternion.Slerp(transform.rotation, rotation, Time.deltaTime * damping);
I've been looking for the same answer, 1+ for you good sir.
Thanks Jesus you help me so much LookPos.y = 0; O$$anonymous$$G i'm crying right now 24 hours of search and you give me the answer :')
Just in case anyone wants their right side to face in a direction as opposed to the front, just add transform.Rotate(0,-90,0) afterwards. It helped me in making my fighting game.
Hi! This answer works....Except for if I want to lock on the X or the Z axis. Am I missing something? When I do "lookPos.x = 0" the object will swivel on another axis after about 180 degree turns.
I want to rotate around a local axis though? Is there any way to do that?
Answer by Revolver2k · May 09, 2012 at 08:58 PM
Hi All,
While Michael's above code should work. Manually modifying transform.rotation is not the recommended Unity way to rotate an object.
It is in fact possible to limit the axes which rotate using transform.LookAt(). Please see the following code which limits rotation to the Y axis only:
Vector3 targetPostition = new Vector3( target.position.x,
this.transform.position.y,
target.position.z ) ;
this.transform.LookAt( targetPostition ) ;
At a basic level the LookAt() function calculates rotational values for the object you want to rotate (transform) based on the positional values of the object you want to look at (target). The key benefit to LookAt() is that it ensures your transform always faces the target, calculating the necessary rotational values accordingly.
All the above code is really saying is: "calculate the rotational values for the transform based on the target's X and Z positions (horizontal plane), but the target's Y position will always be the same as mine." E.g. the same height as me. The resulting calculation forces the transform to face in the direction of the target but believes the target is at the same height of the transform. Thus preventing vertical rotation.
This is my interpretation of it anyway. If anyone can provide further detail I would really welcome it!
[4]: http://answers.unity3d.com/users/5636/oliver-jones.html
That is an awesome answer and explanation. Thanks very much!
How would I limit the lookAt to an arbitrary axis? For instance to surface normal the player is stood on.
for this.
There are many many Questions and answers around this issue and this one is a great answer.
For anyone who is doing 3rd person where character should look at the mouse position like I am doing here is my code which I hope can help!
public Ray $$anonymous$$ousePosition;
void Update () {
$$anonymous$$ousePosition = Camera.main.ScreenPointToRay(Input.mousePosition);
DoLookPlayer($$anonymous$$ousePosition);
}
void DoLookPlayer(Ray mousePos)
{
if (Physics.Raycast(mousePos, out CamRay1Hit, CamRay1Length, CamRay1$$anonymous$$ask.value))
{
Debug.DrawLine (mousePos.origin, CamRay1Hit.point, Color.blue, debugRayLength);
Vector3 LookPosition = new Vector3(CamRay1Hit.point.x,this.transform.position.y, CamRay1Hit.point.z);
transform.LookAt(LookPosition);
}
}
Answer by lodendsg · Jan 03, 2015 at 02:47 AM
Take a look at this (http://answers.unity3d.com/questions/566152/lookat-locked-at-y-axis-but-in-local-space.html)
In summary for those of us that want to rotate for example a turret around its local y axis when said turret can be mounted at an angle in world space the above solution does the trick.
What its doing is similar but projects the point on a plane local to the turret or whatever you are rotating. Below is my implementation for a turret system.
//Project to plane
float distanceToPlane = Vector3.Dot(selfTransform.up, position - selfTransform.position);
Vector3 pointOnPlane = position - (selfTransform.up * distanceToPlane);
selfTransform.LookAt(pointOnPlane, selfTransform.up);
Thanks. Best answer I could find.
And I know It's been a long time, but how could I change this code for it to work around local X axis (or Z axis)? I somehow can't wrap my head around the math behind your code.
Answer by UDN_1fb6f943-d639-43c7-816c-76367e5e91b7 · May 08, 2020 at 11:21 PM
I had similar problem and every answers what i found here arent entirely true, becouse other answers here change X and Z axis too. If you want change only Y axis and do not interfere with others axis, you need do something like this:
var lookPos = target.transform.position - transform.position;
Quaternion lookRot = Quaternion.LookRotation(lookPos);
lookRot.eulerAngles =new Vector3(transform.rotation.eulerAngles.x, lookRot.eulerAngles.y, transform.rotation.eulerAngles.z);
transform.rotation = Quaternion.Slerp(transform.rotation, lookRot, Time.deltaTime * speedOfrotation);
Now objects change only Y axis and can rotate by physics on other Axes.
Thank you! Solved my problem, since I need to edit the other axes.
I did the same, before that I did some research to see if there is something that fits this situation perfectly, but there seems to be no other way.
Answer by moodele · Mar 12, 2017 at 06:09 PM
To look at the object and only rotate at y axis:
void Update()
{
Vector3 lookPos = target.position - transform.position;
Quaternion lookRot = Quaternion.LookRotation(lookPos, Vector3.up);
float eulerY = lookRot.eulerAngles.y;
Quaternion rotation = Quaternion.Euler (0, eulerY, 0);
transform.rotation = rotation;
}
Thank you, that worked in my case... 3D object real-time facing AR camera! I use AR Foundation and it runs smoothly...