using UnityEngine;using 活动:慈云数据爆款香港服务器,CTG+CN2高速带宽、快速稳定、平均延迟10+ms 速度快,免备案,每月仅需19元!! 点击查看System.Collections;/// /// 脚本功能:自动寻路功能/// 脚本位置:移动的主角身上/// 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; } }}
18673888
《unity waypoint》来自互联网同行内容,若有侵权,请联系我们删除!
还没有评论,来说两句吧...