【Unity】Common object movement methods

1 Introduction

        ​​​​Record common game object movement APIs.

2 Mobile API

2.1 MoveTowards

Vector3.MoveTowards

Code:

using UnityEngine;

public class Move : MonoBehaviour
{
    Vector3 targetPos;//定义目的地
    private void Awake()
    {
        //初始化目的地
        targetPos = this.transform.localPosition;
        targetPos.z += 10.0f;
    }
    void Update()
    {
        //匀速运动。
        //返回新的位置,在每帧,将新位置赋值给当前对象,即可实现移动。
        //提供三个参数:当前位置、目标位置、最大移动距离。
        this.transform.localPosition = Vector3.MoveTowards(this.transform.localPosition, targetPos, Time.deltaTime * 3.0f);
    }
}

Demo:

2.2 SmoothDamp

Vector3.SmoothDamp

        There is a problem with this API: the target point can never be reached. Therefore, it is necessary to make a judgment and directly set the object to the target point when it is close to the target point to a certain extent.

Code:

using UnityEngine;

public class Move : MonoBehaviour
{
    Vector3 targetPos;//定义目的地
    Vector3 speed;//定义速度
    private void Awake()
    {
        //初始化目的地
        targetPos = this.transform.localPosition;
        targetPos.z += 10.0f;
        //初始化速度
        speed = Vector3.zero;
    }
    void Update()
    {
        //阻尼运动,慢增慢减的平滑效果。
        //返回新的位置,在每帧,将新位置赋值给当前对象,即可实现移动。
        //可提供五个参数:当前位置、目标位置、用于维护的速度、平滑时间、最大速度。
        //速度:此速度是需要一直维护的,所以不能是局部变量,建议全局变量,此API会根据速度来计算新位置。
        //平滑时间:并不是移动时间,而是用于调整平滑效果的,可以手动测试,选择合适的值。
        this.transform.localPosition = Vector3.SmoothDamp(this.transform.localPosition, targetPos, ref speed, 2.0f, 100.0f);
    }
}

Demo:

2.3 Lerp

Vector3.Lerp

        This is just an interpolation API. It is not actually proposed for movement, but it can be used.
        This API can never reach the target point when moving at a reduced speed (t is a decimal value). Therefore, it is necessary to make a judgment and directly set the object to the target point when it is close to the target point to a certain extent. However, when moving at a constant speed (t eventually equals 1), the target point can be reached.

Code:

using UnityEngine;

public class Move : MonoBehaviour
{
    Vector3 targetPos;//定义目的地
    private void Awake()
    {
        //初始化目的地
        targetPos = this.transform.localPosition;
        targetPos.z += 10.0f;
    }
    void Update()
    {
        //使用插值实现运动。可匀速、可阻尼。
        //返回新的位置,在每帧,将新位置赋值给当前对象,即可实现移动。
        //提供三个参数:值a、值b、插值比t。
        //返回值为a + (b - a) * t。
        this.transform.localPosition = Vector3.Lerp(this.transform.localPosition, targetPos, Time.deltaTime);//此调用方式是减速效果
    }
}

Demo:

Call at a constant speed:

Code:

using UnityEngine;

public class Move : MonoBehaviour
{
    Vector3 startPos;//定义起始位
    Vector3 targetPos;//定义目的地
    float time;//计时,用作插值比t
    float timeControl = 0.5f;//用于控制移动时间
    private void Awake()
    {
        //初始化
        startPos = this.transform.localPosition;
        targetPos = this.transform.localPosition;
        targetPos.z += 10.0f;

    }
    void Update()
    {
        //累计时间,计时
        time += Time.deltaTime;
        //当time * timeControl = 1时,此时time的值即为移动到目标点所需的时间。如这里,所需时间即为2s。
        this.transform.localPosition = Vector3.Lerp(startPos, targetPos, time * timeControl);//匀速
    }
}

Demo:

3 Conclusion

        The code is just a demonstration of the API. Details should be considered for specific use. For example: when the target point cannot be reached, determine the gap and then set the object to the target point; what else should be called in update after reaching the target point; or use a coroutine to move the object and end the coroutine after reaching the end point.

Guess you like

Origin blog.csdn.net/Davidorzs/article/details/134930571