This article mainly introduces the methods of controlling the rotation, movement and scaling of objects in Unity~
Rotation:
(1) Controlling the rotation of objects
public float speed=45;
void Update()
{
transform.Rotate(Vector3.up * speed * Time.deltaTime);
}
(2) Control object A to rotate around object B
Place the script on object A, and then drag object B to the corresponding position of the script.
public float speed=45;
public Transform cube; //物体B
void Update()
{
transform.RotateAround(cube.position, cube.up, speed * Time.deltaTime);
}
move:
Objects keep moving forward
public float speed=2;
void Update()
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
Extension: Use the keyboard (up, down, left and right arrows) to control the object to move forward, backward, left and right
public float speed=3;
void Update()
{
Vector3 v = Input.GetAxis("Vertical") * Vector3.forward;
Vector3 h = Input.GetAxis("Horizontal") * Vector3.right;
transform.Translate(v * speed * Time.deltaTime);
transform.Translate(h * speed * Time.deltaTime);
}
Zoom:
① The object is doubled in size
void Update()
{
transform.localScale = new Vector3(2, 2, 2);
}
② Objects continue to enlarge
void Update()
{
transform.localScale += Vector3.one * Time.deltaTime;
}