unity3D让物体从一个点移动到另一个点

设置一个起始点,终止点,和速度。

一,在Hierarchy面板上右击,选择3D Object----Cube,建立Cube,并将Cube放到物体移动的终点(大小可以调整)

二,右击Project面板,Create----C# Script, 创建move脚本,双击输入:

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

public class move : MonoBehaviour {

    public Transform start;
    public Transform end;
    public float speed;
    void Start()
    {
    }
    void Update()
    {
        transform.position = Vector3.MoveTowards(start.position,end.position,speed * Time.deltaTime);
    }
}

三,将脚本拖动到想要移动的物体的Inspector面板上,Start是要移动的物体,End是我们放置在终点的Cube,Speed速度可以根据需要进行设置。

 这样动画就可以移动了。

原创文章 119 获赞 61 访问量 4万+

猜你喜欢

转载自blog.csdn.net/Ciel_Y/article/details/105620645