【Unity】关于Waypoint的寻路
来源:互联网 发布:suse linux 网关 编辑:程序博客网 时间:2024/06/12 22:33
创建一个名为Path的C#脚本
<span style="font-size:24px;"><span style="font-size:24px;">using UnityEngine;using System.Collections;using System.Collections.Generic;public class Enemy : MonoBehaviour { public float MoveSpeed = 3.0f; //行走速度 private Vector3 Target; //目标位置 private float rotateSpeed = 5.0f; //旋转的速度 public Path path; //Path脚本 private List<Vector3> waypoints; //列表存储waypoints void Awake() { waypoints = new List<Vector3>(); //定义一个waypoint for (int i = 0; i < path.WayPotins.Length; i++) //将Path路径的WayPoints全部数组元素存储到waypoints的列表里 { waypoints.Add(path.WayPotins[i].position); } GetNextPoint(); } void GetNextPoint() //更新下一个waypoint { if (waypoints.Count > 0) //如果当前waypoints里的个数大于0的情况下 { Target = waypoints[0]; //Target就为waypoints的第一个元素 waypoints.RemoveAt(0); //到达目的地后就把当前waypoint列表的第一个元素给删除掉 //以达到更新Target位置的目的 print("路点为:"+waypoints.Count); } }//Use this for initializationvoid Start() {}//Update is called one per framevoid Update() { bool reached = MoveToPoint(Target); if (reached) { GetNextPoint(); }} bool MoveToPoint(Vector3 point) //定义一个布尔类型的移动到目标点的函数 { float distance = Vector3.Distance(this.transform.position,point); if (distance < 0.15f) //如果当前位置与目标位子小于0.15的话,就返回true { return true; } //设置当前目标的旋转为目标减去当前位置的方向 Quaternion wantedRot = Quaternion.LookRotation(point-this.transform.position); //this.transform.rotation = wantedRot; //控制旋转的插值运算 this.transform.rotation = Quaternion.Slerp(this.transform.rotation, wantedRot, rotateSpeed * Time.deltaTime); //移动方向为单位向量 Vector3 dir = (point - this.transform.position).normalized; this.transform.Translate(dir * MoveSpeed * Time.deltaTime, Space.World); return false;//不然就默认返回false }}</span></span>
0 0
- 【Unity】关于Waypoint的寻路
- 【Unity】中WayPoint路点的配置
- unity的自动寻路之 ------ wayPoint寻路的实现方式
- Unity----WayPoint寻路
- Unity3D Waypoint (路点)
- 关于unity的Coroutine
- 关于Unity 的 RenderTexture
- Unity 关于摄像机的想法
- 关于Unity的优化1.
- Unity关于Navigation的教程
- 关于unity的小备注
- 关于unity shader的StencilBuffer
- 关于unity的一些内容
- 关于Unity的版本说明
- Unity--关于Layer的问题
- 关于unity shader的StencilBuffer
- 关于Unity PlayerPrefs的问题
- 【DragonBones&Unity】关于DragonBones载入Unity的问题
- Android基础系列----------- 资源适配
- Linux下的MySQL简单操作(服务启动与关闭、启动与关闭、查看版本)
- KVM Qemu libvirt
- 1071.Speech Patterns
- 1072.Gas Station
- 【Unity】关于Waypoint的寻路
- cookie 和session 的区别详解
- 堆排序(heapsort)
- 深度学习工具包(Deep Learning Toolboxs)
- Android适配器的用法
- Linux显示工作路径
- Visual Studio Warning C4996
- 图论——并查集
- 第十三章:13.4 系统定时任务