0
点赞
收藏
分享

微信扫一扫

Unity3D 基础——使用 Vector3.Lerp 实现缓动效果

祈澈菇凉 2023-10-18 阅读 22

 让一个物体从当前位置移动到另一个位置

 Vector3-Lerp - Unity 脚本 APIicon-default.png?t=N7T8https://docs.unity.cn/cn/current/ScriptReference/Vector3.Lerp.html

1.在场景中新建两个 Cube 立方体,在 Scene 视图中将两个 Cude的位置错开。

 2.新建 C# 脚本 MoveToTarget.cs(写完记得保存)

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

public class MoveToTarget : MonoBehaviour
{
    public Transform endTrans;  //定义结束位置的 Transform 对象

    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        //脚本所在位置缓动到 endTrans 的位置
        transform.position = Vector3.Lerp(transform.position, endTrans.position, Time.deltaTime);
    }
}

3.将脚本绑定到 Cude 上,然后将其 Inpector 视图中将 endTrans 指定为 Cube(1) (我命名的是Arm)。(让A缓动到B,就把脚本绑定在A上,endTrans 设置为 B)

4.点击播放按钮,可以看到 Cube 缓动到 Cube(1)  (我的Arm)的位置

举报

相关推荐

0 条评论