Table of contents
2. Assign a value to Transform.rotation
3. Use the Transform.Rotate function
4. Use the Quaternion.RotateTowards function
5. Use the Transform.LookAt function
6. Transform.RotateAround around the rotation
7.Quaternion.LookRotation gaze rotation
8.Quaternion.FromToRotation Rotate from from to to
1. Three ways of rotation
1. Matrix rotation 2. Euler rotation will cause gimbal lock problem 3. Quaternion rotation can avoid gimbal lock phenomenon
2. Assign a value to Transform.rotation
Rotate 90 degrees around the y-axis
transform.rotation = Quaternion.Euler(0, 90, 0)
3. Use the Transform.Rotate function
transform.Rotate(0,90,0);
4. Use the Quaternion.RotateTowards function
Quaternion targetRotation = Quaternion.Euler(0, 90, 0);
float rotateSpeed = 90f;
transform.rotation = Quaternion.RotateTowards(transform.rotation,
targetRotation, rotateSpeed * Time.deltaTime);
5. Use the Transform.LookAt function
This function will make the object's Z axis point towards the target point, so it can be used when facing the target.
6. Transform.RotateAround around the rotation
transform.RotateAround(Point.position, Vector3.forward, 45);
7.Quaternion.LookRotation gaze rotation
Vector3 lookDir = transform_a.position - transform.position; transform.rotation = Quaternion.LookRotation(lookDir);
8.Quaternion.FromToRotation Rotate from from to to
transform.rotation = Quaternion.FromToRotation(from, to);
9. Pit
The value obtained by transform.localEulerAngles is always a positive number, so when the rotation range is negative, the calculation cannot be called directly
An external variable needs to be defined, such as:
private float euler = 0;
minRot_y = GetRotationY(false);
maxRot_y = GetRotationY(true);
euler = Mathf.Clamp(euler - speed, minRot_y, maxRot_y);
//myCamera.transform.localEulerAngles =new Vector3(myCamera.transform.localEulerAngles.x, eulerY, 0);
myCamera.transform.localRotation = Quaternion.Euler(myCamera.transform.localEulerAngles.x, eulerY, 0);