Unity object movement method

1 Introduction

    In Unity3D , there are many ways to change the coordinates of the object to achieve the purpose of movement. The essence is to modify the position of the object every frame.

2. Move the object through the Transform component

    The Transform component is used to describe the state of the object in space, which includes position, rotation and scale. In fact, all movements will lead to a change in position. The moving of the object through the Transform component mentioned here refers to directly operating the Transform to control the position of the object.

2.1 Transform.Translate

       This method can move the object from the current position to the specified position, and can choose the reference coordinate system. When coordinate system transformation is required, this method can be considered to save the step of transforming the coordinate system

if (Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow)) //上移
{
transform.Translate(Vector3.up * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow)) //下移
{
transform.Translate(Vector3.down * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow)) //左移
{
transform.Translate(Vector3.left * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow)) //右移
{
transform.Translate(Vector3.right * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.E)) //前移
{
transform.Translate(Vector3.forward * speed * Time.deltaTime);
}
if (Input.GetKey(KeyCode.R)) //后移
{
transform.Translate(Vector3.back * speed * Time.deltaTime);
}

2.2 Vector3.Lerp, Vector3.Slerp, Vector3.MoveTowards

      Vector3 can represent either a point in three-dimensional space or a vector. These three methods are all interpolation methods, Lerp is linear interpolation, Slerp is spherical interpolation, and MoveTowards adds the function of limiting the maximum speed on the basis of Lerp. These methods can be considered when it is necessary to move from a given point A to point B.

//Two ways to move from point A to point B: MoveTowards and Lerp
if (Input.GetKey(KeyCode.W))
{ transform.position = Vector3.MoveTowards(transform.position, target.position, stmp); }

if (Input.GetKeyDown(KeyCode.D))
{
transform.position = Vector3.Lerp(transform.position, target.position, stmp);
}

/// <summary>
/// Button click to move
/// </summary>
void Move()
{ transform.position = Vector3.Lerp(transform.position, target.position, speed); }

2.3 Vector3.SmoothDamp

      This method can smoothly move from point A to point B gradually, and can control the speed. The most common usage is that the camera follows the target.

2.4 Transform.position

       Sometimes reassigning position can achieve our goal faster.

3. Move the object through the Rigidbody component

    The Rigidbody component is used to simulate the physical state of the object, such as the object being affected by gravity, the object being knocked off after the collision, and so on.

    Note: All calls about Rigidbody should be placed in the FixedUpdate method, which will be called before each execution of the physics simulation.

3.1 Rigidbody.velocity

     Setting the rigid body velocity allows the object to move and ignore static friction, which allows the object to quickly go from rest to motion.

3.2 Rigidbody.AddForce

      Add a force in one direction to the rigid body, which is suitable for simulating the motion state of the object under the action of external force.

3.3 Rigidbody.MovePosition

      Rigidbody moves to a specified point subject to physical constraints.

4. Move objects through the CharacterController component

     CharacterController is used to control the movement of first-person or third-person characters. In this way, some human behaviors can be simulated, such as limiting the maximum slope of the character's climbing slope, the height of the pace, etc.

4.1 CharacterController.SimpleMove

     It is used to simulate simple movement and automatically apply gravity. The return value indicates whether the character is currently on the ground.

4.2 CharacterController.Move

     To simulate more complex movements, gravity needs to be implemented through code, and the return value represents the collision information between the character and its surroundings.

Guess you like

Origin blog.csdn.net/y18435209887/article/details/131222397