您的位置:首页 > 其它

实现物体的移动效果

2018-01-18 14:48 190 查看
启动Unity应用程序,新建场景MoveToTarget,然后新建一个Cube立方体,再依次选择菜单栏中的GameObject--->Create--->Create Empty,接着在Inspector试图中设置其Transform组件中的position属性的x轴位-6,如下


新建c#脚本MoveToTarget.cs代码如下:using UnityEngine;
using System.Collections;

public class MoveToTarget : MonoBehaviour{
//物体移动的初始位置
private Transform start;
//物体移动的结束为止
private Transform end;

void Start(){
//获取cube
start =GameObject.Find("Cube").GetComponent<Transform>();
//获取空的GameObject
end =GameObject.Find("GameObject").GetComponent<Transform>();
}
void Update(){
transform.position = Vector3.Lerp(start.position,end.position,Time.deltaTime);
}
}将上面的MoveToTarget.cs脚本绑定到Cube游戏对象上,然后单击工具栏中的播放按钮就可以看到Cube立方体缓缓移动到GameObject对象的位置
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: