1. The rotation of the object
Give the object a rotation angle.
1. Quaternion quadruple (x, y, z, w)
transfrom.rotation()=... is inconvenient to operate, and it is not recommended to use it officially
2. Euler Angle
transfrom.eulerAngles = new Vector(0,45,0);
transfrom.LocalE ulerAngles = new Vector(0,45,0);
void Start(){
transfrom.localEulerAngles = new Vector(0,45,0);
}
This will rotate the object 45 degrees.
Modify the angle in Update and continue to rotate
Vector3 angles = transfrom.localEulerAngles;
anlges.y +=0.5f;
transfrom.localEulerAngles = angles;
Optimized to rotate at a constant speed
float rotateSpeed = 30;
void Update(){
Vector3 angle = transfrom.localEulerAngles;
angle.y +=0.5f;
transfrom.localEulerAngles = angle;
}
Uniform rotation:
void Update(){
float rotateSpeed = 30;
Vector3 angle = transfrom.localEulerAngles;
angle.y +=rotateSpeed * Time.deltaTime;
transfrom.localEulerAngles = angle;
}
2. Relative rotation
Rotate(): Rotate a relative angle
transfrom.Rotate(dx,dy,dz,space)
void Update(){
float rotateSpeed = 30;
float speed = rotateSpeed * Time.deltaTime;
this.transfrom.Rotate(0,speed,0,Space.Self);
}
3. Rotation and revolution
Rotation: rotating around its own axis
Revolution: revolve around another object
When the parent object rotates, the child object rotates with it.
Example: A satellite orbits the earth
viod Update(){
float rotateSpeed = 60;
float speed = rotateSpeed * Time.deltaTime;
Transfrom parent = this.transfrom.parent;
parent.Rotate(0,speed,0,Space.Self);
}
Find the parent object of the object that needs to be revolved, and let the parent object rotate.
Make the earth rotate around the y-axis of the earth, set the initial position of the earth to 0 (same as the position of the earth-moon system), and the satellite revolves around the object a1, so that the rotation of a1 can drive the satellite to rotate. Since the rotation points around which the earth and the satellite revolve are different, their angular velocities can be adjusted separately.
4. Official documents
unity.cn
-manual
-Script API
Unity User Manual 2021.3 (LTS) - Unity Manual
Unity also has an English document when it is installed
Address: Uhub\2021.3.18f1c1\Editor\Data\Documentation\en