How does Unity smooth motion - movement and motion mode (with general implementation code)

Introduction of basic knowledge

There are 2 ways to move objects

  • Non-physical movement (no factors such as force/speed), only need to change the position of the object  
  • Physical movement, this time you need to use the physics system in Unity

There are 3 types of exercise

1. to move in one direction

non-physical movement

In Update, add the (x, y, z) value in Transform.position, for example, Transform.position.x+=0.1f, 0.1f is called the step size

Physical movement ( method in Rigidbody component )

  • The Rigidbody.AddForce() method applies force in a certain direction    
  • Rigidbody.velocity gives the object a movement velocity    
  • The method in CharacterController, due to the Rigidbody property of the inherited part in CharacterController, can also control the movement of objects, mainly used to make the first and third-person shooting movements  

2. Move towards the target point (usually the mouse click point)

non-physical movement

  1. Transform.Translate: For example, Transform.Translate(Vector3.zero*Time.deltaTime) moves towards (0,0,0) at a speed of one meter per second    
  2. Vector3.lerp: Vector.Slerp, Vector3.MoveTowards interpolation. Vector3.lerp(transform.position, targetposition, Time.deltaTime), Slerp is mainly used for interpolation of angle radians, and MoveTowards adds a limit to the maximum speed on the basis of Lerp    
  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.

physical movement

  • The Rigidbody.MovePosition method in Righdbody is used to move to the target point   

3. From point A to point B (specialization of motion mode 2)


use

When an object is in motion, there will be a velocity variable, which is our desired velocity. In the process from rest to motion, there will be an acceleration. In game development, it is necessary to achieve a smooth movement of an object, that is, to simulate the process of reaching the desired speed from rest through acceleration. The game runs on a frame-by-frame basis. At the beginning, the target movement speed is 0. With the passage of each frame, the target speed will continuously accumulate the acceleration in one frame. When the accumulation reaches the desired speed, it will come at the desired speed. sports.

The code implemented in Unity is as follows (the principle is common to all game engines)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MovingSphere : MonoBehaviour
{
    // 用户设定最大期望速度
    [SerializeField, Range(0f, 100f)] private float maxSpeed = 10f;
    // 用户设定最大加速度
    [SerializeField, Range(0f, 100f)] private float maxAcceleration = 10f;

    // 当前的运动速度
    private Vector3 velocity;

    // Update is called once per frame
    void Update()
    {
        // 通过读取用户输入,来确定期望速度向量(向量包含了大小和方向)
        Vector2 playerInput;
        playerInput.x = Input.GetAxis("Horizontal");
        playerInput.y = Input.GetAxis("Vertical");
        Vector3 desiredVelocity = new Vector3(playerInput.x, playerInput.y, 0.0f) * maxSpeed;

        // 计算这一帧中的最大加速度(现实中是以秒为单位,而游戏运行是以帧为单位)
        float maxSpeedChanged = maxAcceleration * Time.deltaTime;

        // 下面的代码是将当前速度限制为最大期望速度
        if (velocity.x < desiredVelocity.x)
        {
            velocity.x = Mathf.Min(velocity.x + maxSpeedChanged, desiredVelocity.x);
        }
        else if (velocity.x > desiredVelocity.x)
        {
            velocity.x = Mathf.Max(velocity.x - maxSpeedChanged, desiredVelocity.x);
        }

        if (velocity.y < desiredVelocity.y)
        {
            velocity.y = Mathf.Min(velocity.y + maxSpeedChanged, desiredVelocity.y);
        }
        else if (velocity.y > desiredVelocity.y)
        {
            velocity.y = Mathf.Max(velocity.y - maxSpeedChanged, desiredVelocity.y);
        }

        // 得到了当前速度,就可以计算当前这一帧的位移
        Vector3 displacement = velocity * Time.deltaTime;

        // 将物体的当前位置,累加上这一帧的位移,就是最终的移动
        transform.localPosition += displacement;
    }
}

Guess you like

Origin blog.csdn.net/Luoxiaobaia/article/details/123864221