var speed = 3.0; //速度
var rotationSpeed = 5.0; //旋转速度
var shootRange = 20.0; //判断周围F的队列
var attackRange = 50.0; //攻击队列
var shootAngle = 4.0; //发射角度
var dontComeCloserRange = 5.0; //结束范围
var delayShootTime = 0.45; //延迟的时间
private var pickNextWaypointDistance = 0.0; //获取下一个路点的距离
var maxstop = 3; //停止
var target : Transform; //目标的坐标点
var command : String = "Stay"; //停止的动作
private var lastShot = -10.0; //最后的发射
private var controller; //控制器
// Make sure there is always a character controller
@script RequireComponent (CharacterController)
function Start () {
waypointPosition = transform.position; //路点位置等于当前人物的位置
command = "Stay"; //动画是停留
controller = GetComponent (CharacterController); //获取控制器
Patrol(); //巡逻函数
yield WaitForSeconds (Random.value*0.5); //等待随机一个时间
while (true) { //无限循环
FindPoint (curpoint); //搜索路点
yield WaitForSeconds (0.2); //等待0.2秒
}
}
private var waypointPosition : Vector3; //路点位置
var cst = false;
var points : Vector3[]; //点的数 组
var curpoint = 0; //坏点
private var move_margin = 3.5; //移动距离
function Update () {
//Debug.color = Color.blue;
Debug.DrawLine (transform.position, waypointPosition, Color.blue); //当前人物向路点发射一条蓝光
}
function SetPoints (newPoints : Vector3[]) {
points = newPoints; //新的坐标点
FindPoint (0); //传入FindPoint一个0的值
command = "Walk"; //动画为走路
}
function FindPoint (cpoint : int) {
curpoint = cpoint; //把cpoint的值给curpoint
if (!points || points.length == 0 || curpoint >= points.length) { //
waypointPosition = transform.position; //把当前位置给路点的位置
Stop (); //停止函数
return; //退出此函数
}
if (points.length == 1) { //点数组的长度为一时
waypointPosition = points[0]; //把点数组的第一个值给路点
command = "Walk"; //步行的动作
return; //退出此函数
}
command = "Walk"; //步行的动作
waypointPosition = points[curpoint]; //点数组的curpoint值给路点
p = waypointPosition; //p的位置等于路点的位置
p.y = transform.position.y; //p的y坐标等于人物的y坐标值
if (Vector3.Distance (transform.position,p) < dontComeCloserRange) { //人物的坐标与p点的距离小于不到达关闭的范围
curpoint++; //curpoint的值加加
}
}
function Patrol () { //巡逻函数
//var curWayPoint = AutoWayPoint.FindClosest(transform.position);
while (true) {
if (command == "Walk") { //步行动画
MoveTowards(waypointPosition); //面向路点的位置
}
yield;
}
}
function Stop () { //停止函数
command = "Stay"; //停留的动画
SendMessage("SetSpeed", 0.0, SendMessageOptions.DontRequireReceiver); //发0.0给的SetSpeed函数
}
function RotateTowards (position : Vector3) { //旋转方向
SendMessage("SetSpeed", 0.0); //发给有一个SetSpeed参数的函数
var direction = position – transform.position; //方向
direction.y = 0; //方向的Y坐标的值为0
if (direction.magnitude < 0.1) //方向的大小小于0.1返回
return;
// Rotate towards the target
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); //旋转插值
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0); //人物的角
}
function MoveTowards (position : Vector3) { //移动瞧向
var direction = position – transform.position; //瞧向传入的位置的方向
direction.y = 0; //向量的y=0
if (direction.magnitude <0.5) { //向量的范围小于0.5
Stop (); //执行停止函数
return; //退出此函数
}
// Rotate towards the target
transform.rotation = Quaternion.Slerp (transform.rotation, Quaternion.LookRotation(direction), rotationSpeed * Time.deltaTime); //当前点的向量向看的方向的向量的插值步长是每秒rotationSpeed
transform.eulerAngles = Vector3(0, transform.eulerAngles.y, 0); //当前的角
// Modify speed so we slow down when we are not facing the target 因此,我们修改的速度减慢,当我们没有面对目标
var forward = transform.TransformDirection(Vector3.forward); //把人物前方的方向转换为世界坐标上的方向
var speedModifier = Vector3.Dot(forward, direction.normalized); //人物前面为正人物后面为负
speedModifier = Mathf.Clamp01(speedModifier); //转换为0到1之间的值
// Move the character
direction = forward * speed * speedModifier; //移动的速度
controller.SimpleMove(direction); //移动
SendMessage("SetSpeed", speed * speedModifier, SendMessageOptions.DontRequireReceiver); //发给SetSpeed的函数
}