首页 > 编程知识 正文

unity waypoint

时间:2023-05-04 07:46:18 阅读:274368 作者:2686

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; } }}

版权声明:该文观点仅代表作者本人。处理文章:请发送邮件至 三1五14八八95#扣扣.com 举报,一经查实,本站将立刻删除。