public List<Vector3> List_PathPoint = new List<Vector3>(); public float MoveSpeed = 5f; private int currentPathIndex = 0; void Update() { // 获取当前指向的路径点 Vector3 currentTarget = List_PathPoint[currentPathIndex]; // 计算移动方向和距离 Vector3 moveDirection = currentTarget - transform.position; float distanceToTarget = moveDirection.magnitude; // 如果距离小于可以接受的误差,则移动到下一个路径点 if (distanceToTarget < 0.1f) { currentPathIndex++; if (currentPathIndex >= List_PathPoint.Count) { currentPathIndex = 0; } } else { // 向下一个路径点移动 Vector3 moveVector = moveDirection.normalized * MoveSpeed * Time.deltaTime; transform.position += moveVector; } }
您需要 登录 才可以下载或查看,没有账号?立即注册
使用道具 举报
本版积分规则 发表回复 回帖并转播 回帖后跳转到最后一页
小黑屋|手机版|Unity开发者联盟 ( 粤ICP备20003399号 )
GMT+8, 2024-11-16 16:30 , Processed in 0.088787 second(s), 26 queries .
Powered by Discuz! X3.5 Licensed
© 2001-2024 Discuz! Team.