- Home /
to rotate along z axis
i need the camera to rotate along z axis as the player moves left/right,currently the camera rotates along y axis (since i am using lookrotation).How can i modify code to rotate along z axis...m bit weak in maths facepalm;/
public Transform target; public float tiltAngle;
void Update()
{
Vector3 relativePos =target.position - transform.position;
transform.rotation=Quaternion.RotateTowards(transform.rotation,Quaternion.LookRotation(relativePos),Mathf.Deg2Rad*tiltAngle);
}
Answer by robertbu · Aug 04, 2014 at 07:35 AM
I'm not sure of your Mathf.Deg2Rad*tiltAngl and how that maps to your angle. Here is some code that points in the right direction. You might need to add or subtract from angle (usually 90 degrees) before using it in AngleAxis() to get the rotation you want:
void Update()
{
Vector3 relativePos =target.position - transform.position;
float angle = Mathf.Atan2(relativePos.y, relativePos.x) * Mathf.Rad2Deg;
transform.rotation=Quaternion.RotateTowards(transform.rotation,Quaternion.AngleAxis(angle,transform.forward),Mathf.Deg2Rad*tiltAngle);
}
i subtracted 90 from angle before its use in AngleAxis(),the camera on which this code is applied does a 180 rotation even before i move my player along the x axis(when i set tilt angle to 10 and nuthing happens when tilt angle is zero)