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.
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:
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!
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);
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:
If anyone is still having issues with a quad billboard not following the main camera without it pitching up or down, here is the code that worked for me. I used this for flags on a terrain that has a slight top down camera. Only the Y axis will rotate on your billboard! =)
NOTE: If your billboards are facing the wrong direction, change the (-posCam) into (posCam).
using UnityEngine;
using System.Collections;
public class RulerFlag : MonoBehaviour {
public Transform camCam;
public Vector3 posCam;
public Quaternion lastPos;
public float turnSpeed = 20f;
void Awake ()
{
camCam = Camera.main.transform;
}
void Update ()
{
posCam = camCam.position - transform.position;
posCam.y = 0;
lastPos = Quaternion.LookRotation(-posCam);
transform.rotation = Quaternion.Slerp(transform.rotation,lastPos, Time.deltaTime * turnSpeed);
}
}
Credit to poster “Mike 3” for the meat of this code. I’ve only filled in the rest of the code so that everyone can copy and past this into a script file and drop it onto any object. Cheers!