百度免费网站制作,比较好的做外贸网站,软件搭建,中通物流企业网站建设书注#xff1a;本文用到了前文所用的基类UnityAI——操控行为编程的主要基类-CSDN博客
在一些游戏中#xff0c;可能会遇到想让AI角色追逐或者避开玩家的情况。
如在飞机模拟游戏中#xff0c;让导弹跟踪和进攻玩家或玩家的飞行器。这种情况下#xff0c;可以运用本节介绍…注本文用到了前文所用的基类UnityAI——操控行为编程的主要基类-CSDN博客
在一些游戏中可能会遇到想让AI角色追逐或者避开玩家的情况。
如在飞机模拟游戏中让导弹跟踪和进攻玩家或玩家的飞行器。这种情况下可以运用本节介绍的技术。
一、靠近
操控行为中的靠近是指指定一个目标位置根据当前的运动速度向量返回一个操控AI角色到达该目标位置的操控力使AI角色自动向该位置移动。
要想让AI角色靠近目标首先要计算出AI角色在理想情况下到达目标位置的预期速度。该预期速度可看作是从AI角色的当前位置到目标位置的向量。操控向量是预期速度与AI角色当前速度的差该向量大小随着当前位置的变化而变化从而形成角色的寻找路径。 由于操控行为是基于力和速度的速度不会突变因此如果角色一直采取靠近行为那么最终它将会从目标穿过然后再重新接近目标如此往复。 当然如果目标物体包含一个碰撞体那么接下来的行为就要由Character Controller决定了。如果不希望出现这种情况可以采用后面介绍的Arrive行为也可以另外添加一些处理步骤。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SteeringForSeek : Steering
{public GameObject target;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;private bool isPlanar;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;isPlanar m_vehicle.isPlanar; }public override Vector3 Force(){desiredVelocity (target.transform.position - transform.position).normalized * maxSpeed;if (isPlanar)desiredVelocity.y 0;return (desiredVelocity - m_vehicle.velocity);}}二、离开
离开和靠近行为正好相反它会产生一个操控AI角色离开目标的力而不是靠近目标的力。它们之间唯一的区别是DeisredVelocity具有相反的方向。
接着还可以进一步调整只有当AI角色进入目标周围一定范围内时才产生离开的力这样可以模拟出AI角色的有限感知范围
这里采用了Vector3.Distance函数来计算当前位置与目标位置之间的距离。事实上如果采用Vector3.sqrMagnitude函数将会得到更快的计算速度因为省去了计算平方根的时间这时可以预先计算fearDistance的平方并存储到一个变量中。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SteeringForFlee : Steering
{public GameObject target;public float fearDistance 20;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;}public override Vector3 Force(){Vector3 temPos new Vector3(transform.position.x, 0, transform.position.z);Vector3 temTargetPos new Vector3(target.transform.position.x, 0, target.transform.position.z);if (Vector3.Distance(temPos, temTargetPos) fearDistance)return new Vector3(0, 0, 0);desiredVelocity (transform.position - target.transform.position).normalized * maxSpeed;return (desiredVelocity-m_vehicle.velocity);}
}三、抵达
有时我们希望AI角色能够减速并停到目标位置避免冲过目标例如车辆在接近十字路口时逐渐减速然后停在路口处这时就需要用到抵达行为。
在角色距离目标较远时抵达与靠近行为的状态是一样的但是接近目标时不再是全速向目标移动而代之以使AI角色减速知道最终恰好停在目标位置。何时开始减速是通过参数进行设置的这个参数可以看成是停止半径。当角色在停止半径之外时以最大速度移动当角色在停止半径之内时逐渐减小预期速度直到减小为0.这个参数的设置很关键它决定了抵达行为的最终效果。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SteeringForArrive : Steering
{public bool isPlanar true;public float arrivalDistance 0.3f;public float characterRadius 1.2f;public float slowDownDistance;public GameObject target;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;isPlanar m_vehicle.isPlanar;}public override Vector3 Force(){Vector3 toTarget target.transform.position - transform.position;Vector3 desiredVelocity;Vector3 returnForce;if (isPlanar)toTarget.y 0;float distance toTarget.magnitude;if (distance slowDownDistance){desiredVelocity toTarget.normalized * maxSpeed;returnForce desiredVelocity - m_vehicle.velocity;}else{desiredVelocity toTarget - m_vehicle.velocity;returnForce desiredVelocity - m_vehicle.velocity;}return returnForce;}void OnDrawGizmos(){Gizmos.DrawWireSphere(target.transform.position, slowDownDistance);}
}
四、追逐
追逐行为与靠近行为很相似只不过目标不再是静止不动而是另一个可移动的角色。最简单的追逃方式是直接向目标的当前位置靠近不过这样看上去很不真实。举例来说众所周知当动物追猎物时不是直接向猎物当前的位置奔跑而是要预判朝着其未来位置的方向追去这样才能在最短时间内追上猎物。在AI中把这种操控行为称为追逐。
如何实现这种智能的追逐行为呢我们可以使用一个简单的预测器在每一帧重新计算它的值。
假设采用一个线性预测器有假设在预测时间间隔T时间内角色不会转向角色经过时间T后的未来位置可以用当前速度乘以T来确定然后把得到的值加到角色当前位置上就得到未来位置了。最后再以预测位置作为目标应用靠近行为就可以了。 实现追逐行为的一个关键是如何确定T。可以把它设置为一个常数也可以当追逐者距离目标较远时设为较大的值而接近目标时设为较小的值。 这里设定预测时间和追逐者与逃避者之间的距离成正比与二者的速度成正比。
一些情况下追逐可能会提前结束。例如逃避者在前面几乎面对追逐者那么追逐者应该直接向逃避者的当前位置移动。二者之间的关系可以通过计算逃避者朝向向量与AI角色朝向向量的点积得到在下面的代码中逃避者朝向的反向和AI角色的朝向必须大约在20度范围之内才可以被认为是面对着的。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SteeringForPersuit : Steering
{public GameObject target;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;}public override Vector3 Force(){Vector3 toTarget target.transform.position - transform.position;float relativeDirection Vector3.Dot(transform.forward, target.transform.forward);//计算追逐者的前方与逃避者前方之间的夹角if (Vector3.Dot(toTarget, transform.forward) 0 relativeDirection -0.95f)//夹角大于0且追逐者基本面对着逃避者{desiredVelocity(target.transform.position - transform.position).normalized*maxSpeed;return (desiredVelocity - m_vehicle.velocity); }float lookaheadTime toTarget.magnitude / (maxSpeed target.GetComponentVehicle().velocity.magnitude);//计算预测时间正比于距离反比于速度和desiredVelocity (target.transform.position target.GetComponentVehicle().velocity * lookaheadTime - transform.position).normalized * maxSpeed;return(desiredVelocity - m_vehicle.velocity);}
}五、逃避
逃避行为与追逐行为的不同是它试图使AI角色逃离预测位置。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SteeringForEvade :Steering
{public GameObject target;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;}public override Vector3 Force(){Vector3 toTarget target.transform.position - transform.position;float lookaheadTime toTarget.magnitude / (maxSpeed target.GetComponentVehicle().velocity.magnitude);//向前预测的时间desiredVelocity (transform.position - (target.transform.positiontarget.GetComponentVehicle().velocity*lookaheadTime)).normalized * maxSpeed;return (desiredVelocity - m_vehicle.velocity);}
}六、随机徘徊
很多时候我们需要让游戏中的角色随机移动比如说士兵的巡逻。我们希望这种随机移动看上去是真实的而不是一直循环某个路径。
利用操控行为来实现随机派啊坏有多种不同的方法最简单的方式是利用前面提到的靠近行为。在场景中随机放置目标让角色靠近目标每隔一定时间就随机改变目标的位置。这个方法很简单但缺点也很明显比如角色可能会突然掉头因为目标可能移动到了角色的后面。
解决这个问题的原理与内燃机的气缸曲轴转动相似。在角色气缸通过连杆连接到曲轴上目标被限定到曲轴圆周上移向目标。为了看得更随机每帧给目标附加一个随机的位移。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SteeringForWander : Steering
{public float wanderRadius; //徘徊半径public float wanderDistance; //徘徊距离public float wanderJitter; //每秒加到目标的随即位移的最大值public bool isPlanar;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;private Vector3 circleTarget;private Vector3 wanderTarget;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;isPlanar m_vehicle.isPlanar;circleTarget new Vector3(wanderRadius * 0.707f, 0, wanderRadius * 0.707f); //选取与安全上的一个点作为初始点}public override Vector3 Force(){Vector3 randomDisplacement new Vector3((Random.value - 0.5f) * 2 * wanderJitter, (Random.value - 0.5f) * 2 * wanderJitter, (Random.value - 0.5f) * 2 * wanderJitter);if (isPlanar)randomDisplacement.y 0;circleTargetrandomDisplacement;//将随机位移加到初始点上circleTarget wanderRadius * circleTarget.normalized;//由于新位置很可能不在圆周上因此需要投影到圆周上wanderTarget m_vehicle.velocity.normalized * wanderDistance circleTarget transform.position;//之前计算出的值是相对于AI的需要转换为世界坐标desiredVelocity (wanderTarget - transform.position).normalized * maxSpeed;return (desiredVelocity - m_vehicle.velocity);}
}七、路径跟随
就像赛道上的赛车需要导航一样路径跟随会产生一个操控力使AI角色沿着由事先设置的轨迹构成路径的一系列路点移动。
最简单的方式是将当前路点设置为路点列表中的第一个路点用靠近行为来靠近这个路点至非常接近这个点然后靠近列表中的下一个路点一直到最后一个路点。
在实现这一功能时需要设置一个路点半径参数这个参数的设置会引起路径形状的变化
using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SteeringFollowPath : Steering
{public GameObject[] waypoints new GameObject[4];//由节点表示的路径private Transform target;private int currentNode;private float arriveDistance;private float sqrArriveDistacne;private int numberOfNodes;private Vector3 force;private Vector3 desiredVelocity;//预期速度private Vehicle m_vehicle;//获得被操控的AI角色private float maxSpeed;private bool isPlanar;public float slowDownDistacne;void Start(){numberOfNodes waypoints.Length;m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;isPlanar m_vehicle.isPlanar;currentNode 0;target waypoints[currentNode].transform;arriveDistance 1.0f;sqrArriveDistacne arriveDistance * arriveDistance;}public override Vector3 Force(){force new Vector3(0, 0, 0);Vector3 dist target.position - transform.position;if (isPlanar)dist.y 0;if(currentNodenumberOfNodes-1){if(dist.magnitudeslowDownDistacne){desiredVelocitydist.normalized*maxSpeed;forcedesiredVelocity-m_vehicle.velocity;}else{desiredVelocity dist - m_vehicle.velocity;force desiredVelocity - m_vehicle.velocity;}}else{if(dist.sqrMagnitudesqrArriveDistacne){currentNode;target waypoints[currentNode].transform;}desiredVelocity dist.normalized * maxSpeed;force desiredVelocity - m_vehicle.velocity;}return force;}
}八、避开障碍
避开障碍是指操控AI角色避开路上的障碍物例如在路径上有一颗树当角色距离树比较近时就会产生一个排斥力使AI角色不至于撞上树。当有好几棵树时至产生躲避最近的树的操控力这样AI角色就会一个一个地躲开这些树。
在这个算法中首先需要发现障碍物。AI角色唯一需要担心的就是挡在其路线前方的那些物体。算法的分析步骤如下 1、用角色前进的速度生成一个向亮ahead aheadpositionnormalize(velocity)*MAX_SEE_AHEAD。ahead的长度决定了AI能看到的距离 2、每个障碍物都用一个几何形状表示这里采用包围球来标识场景中的每个障碍。 一种可能的方法是检测ahead向量与障碍物的包围球是否相交。这里采用简化的方法。 需要一个向量ahead2ahead2ahead*0.5 3、接下来进行碰撞检测。只需要比较向量的终点与球心的距离d是否小于球的半径。如果ahead与ahead2中的一个向量在球内那么说明障碍物在前方。如果监测到了多个障碍物那么选择最近的那个。 4、计算操控力 avoidance_forceahead-obstacle_center avoidance_forcenormalize(avoidance_force)*MAX_AVOID_FORCE 采用这种方法的缺点是当AI角色接近障碍而操控力正在使其原理的时候即使AI正在旋转也可能会检测到碰撞。一种改进方法是根据AI角色的当前速度调整ahead向量计算方法如下 Dynamic_lengthlength(velocity)/MAX_VELOCITY aheadpositionnormalize(velocity)*dynamic_length 这时dynamic_length的范围是01当全速移动时值是1 using System.Collections;
using System.Collections.Generic;
using UnityEngine;public class SteeringForCollisionAvoidance :Steering
{public bool isPlanar;private Vector3 force;private Vector3 desiredVelocity;//Ԥٶprivate Vehicle m_vehicle;//ñٿصAIɫprivate float maxSpeed;private float maxForce;public float avoidanceForce;public float MAX_SEE_AHEAD 2.0f;//ܼprivate GameObject[] allColliders;void Start(){m_vehicle GetComponentVehicle();maxSpeed m_vehicle.maxSpeed;isPlanar m_vehicle.isPlanar;maxForce m_vehicle.maxForce;if(avoidanceForcemaxForce)avoidanceForce maxForce;allColliders GameObject.FindGameObjectsWithTag(obstacle);//洢TagΪobstacleΪײ}public override Vector3 Force(){RaycastHit hit;Vector3 force new Vector3(0, 0, 0);Vector3 velocity m_vehicle.velocity;Vector3 normalizedVelocity velocity.normalized;Debug.DrawLine(transform.position, transform.position normalizedVelocity * MAX_SEE_AHEAD * (velocity.magnitude / maxSpeed));//һߣҪཻײif (Physics.Raycast(transform.position, normalizedVelocity, out hit, MAX_SEE_AHEAD * velocity.magnitude / maxSpeed)){Vector3 ahead transform.position normalizedVelocity * MAX_SEE_AHEAD * (velocity.magnitude / maxSpeed); //ijײཻʾײforceahead-hit.collider.transform.position;//ײIJٿforce * avoidanceForce;if (isPlanar)force.y 0;foreach(GameObject c in allColliders)//ıײɫ{if (hit.collider.gameObject c){c.GetComponentRenderer().material.color Color.black;}elsec.GetComponentRenderer().material.color Color.white;}}else//ǰûмײ壬ײıɫ{foreach (GameObject c in allColliders){c.GetComponentRenderer().material.color Color.white;}}return force;}
}