Unity中WayPoint路点的配置
2015-09-23 17:43
429 查看
using UnityEngine; using System.Collections; /// <summary> /// 脚本功能:自动寻路功能 /// 脚本位置:移动的主角身上 /// </summary> public class WayPoint : MonoBehaviour { // 每个路点设置为Trigger。 // 移动主角添加Rigibody属性。 // 这里使用了拖拽方式,如果路点的名字是带有数字,并且连续的,可以使用for循环代码加载 public Transform[] WayPoints; // 移动速度 private float MoveSpeed = 3f; // 当前人物需要移动的方向向量 Vector3 direction; // 下一个路点的索引 private int nextIndex; void Start () { nextIndex = 0; } void Update () { // 通过向量减法,算出朝向下一个路点的方向向量 direction = (WayPoints [nextIndex].position - transform.position).normalized; transform.Translate(direction * MoveSpeed * Time.deltaTime); } void OnTriggerEnter (Collider other) { nextIndex++; Debug.Log (nextIndex); // 这里我使用了4个路点,会循环,所以每次到最后一个点就会回到起点 // 大家可以根据自己的需求更改代码 if (nextIndex >= 4) { nextIndex = 0; } } }
相关文章推荐
- 在Unity中使用Newtonsoft.Json来生成和读取json
- 在Unity中使用Newtonsoft.Json来生成和读取json
- Unity3D:spine使用
- Unity3D中uGUI事件系统简述及使用方法总结
- Unity技巧2
- Unity技巧1
- 简化Unity开发的利器coroutine
- Unity3D之Mecanim动画系统学习笔记(七):IK(反向动力学)动画
- Unity中用路点画线
- unity学习前篇
- Unity3d UGUI 动态加载按钮
- 在Unity3D中实现安卓平台的本地通知推送
- Unity3D:NGUI UIGrid 排序、动态加载与销毁
- Unity3d中的属性(Attributes)整理
- Unity导入FBX自动进行动画切分
- Unity导入FBX自动进行动画切分
- 所谓的Unity多线程
- unity5 人皮渲染 Skin Shading
- 最近用unity5弄的一些渲染
- Unity3D学习笔记(十八)使用插件NGUI制作小地图