I have a turret I would like to automatically look at specific targets, rotating only on the Z-axis, looking up. Most of the examples I see of LookRotation are in Javascript, which does Quaternions differently than C#. In JS you can list the axes seperately but you can’t go that in C#. Example:
public Transform target;
void Update () {
var newRotation = Quaternion.LookRotation(target - transform.position).eulerAngles;
newRotation.x = 0;
newRotation.y = 0;
transform.rotation = Quaternion.Euler(newRotation);
}
Isn’t as simple in C#. Quaternions are pretty confusing to me so I was just wondering how to limit the rotation to just Z-axis in C#.
I believe this is what you are looking for:
public Transform target;
void Update () {
Vector3 look = target.position - transform.position;
look.z = 0;
transform.rotation = Quaternion.LookRotation(look);
}