接上一篇,这篇讲AI控制。
using System;
using UnityEngine;
using UnityEngine.AI;
using Random = UnityEngine.Random;
namespace UnityStandardAssets.Vehicles.Car
{
//添加关联脚本
[RequireComponent(typeof (CarController))]
public class CarAIControl : MonoBehaviour
{
//枚举刹车条件
public enum BrakeCondition
{
NeverBrake, // the car simply accelerates at full throttle all the time.汽车一直以最高速度行驶
//车会根据在朝着目标方向的即将发生的变化刹车,在转向时减速,在基于路线的AI
TargetDirectionDifference, // the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.
//车会在接近目标时刹车,不管目标的方向,对如果你想前往一个静止的目标,当到达那里时休息
TargetDistance, // the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
// head for a stationary target and come to rest when it arrives there.
}
//该脚本以与用户控件脚本相同的方式为控制器提供输入,正因如此,它实际是“驾驶”汽车,没有特殊的物理和动画技巧,使得汽车的行为正常。
// This script provides input to the car controller in the same way that the user control script does.
// As such, it is really 'driving' the car, with no special physics or animation tricks to make the car behave properly.
//“漫游”是用来给汽车一个更人性化,更少的机器人感觉。它们在向目标方向行驶时,速度和方向都有轻微的抖动。
// "wandering" is used to give the cars a more human, less robotic feel. They can waver slightly
// in speed and direction while driving towards their target.
//[Range(0,1)]使得变量范围为0~1;
//最谨慎的时候使用最大速度的百分比
//[SerializeField] private BoxCollider Check;
[SerializeField] [Range(0, 1)] private float m_CautiousSpeedFactor = 0.05f; // percentage of max speed to use when being maximally cautious
//接近转弯的角度去当作保证最大的谨慎去处理
[SerializeField] [Range(0, 180)] private float m_CautiousMaxAngle = 50f; // angle of approaching corner to treat as warranting maximum caution
//最大安全距离
[SerializeField] private float m_CautiousMaxDistance = 100f; // distance at which distance-based cautiousness begins
//AI应该考虑它的角速度(例如旋转时放开加速度)谨慎使用
[SerializeField] private float m_CautiousAngularVelocityFactor = 30f; // how cautious the AI should be when considering its own current angular velocity (i.e. easing off acceleration if spinning!)
//AI如何使用转向输入转向期望的方向。
[SerializeField] private float m_SteerSensitivity = 0.05f; // how sensitively the AI uses steering input to turn to the desired direction
//AI如何使用加速器达到当前所需的速度
[SerializeField] private float m_AccelSensitivity = 0.04f; // How sensitively the AI uses the accelerator to reach the current desired speed
//AI如何使用制动器达到当前所需的速度
[SerializeField] private float m_BrakeSensitivity = 1f; // How sensitively the AI uses the brake to reach the current desired speed
//汽车将朝着目标的方向移动多远?
[SerializeField] private float m_LateralWanderDistance = 3f; // how far the car will wander laterally towards its target
//汽车侧移速度有多快
[SerializeField] private float m_LateralWanderSpeed = 0.1f; // how fast the lateral wandering will fluctuate
//汽车加速有多少位移
[SerializeField] [Range(0, 1)] private float m_AccelWanderAmount = 0.1f; // how much the cars acceleration will wander
//汽车加速移动会有多少波动
[SerializeField] private float m_AccelWanderSpeed = 0.1f; // how fast the cars acceleration wandering will fluctuate
//AI在加速制动时应该考虑什么
[SerializeField] private BrakeCondition m_BrakeCondition = BrakeCondition.TargetDistance; // what should the AI consider when accelerating/braking?
//汽车是否正在积极的驱动或者停止
[SerializeField] private bool m_Driving; // whether the AI is currently actively driving or stopped.
//目标地点
[SerializeField] private Transform m_Target; // 'target' the target object to aim for.
//会在接近目标的时候停止行驶?
[SerializeField] private bool m_StopWhenTargetReached; // should we stop driving when we reach the target?
//接近目标考虑我们'到达',并停止驾驶。
[SerializeField] private float m_ReachTargetThreshold = 2; // proximity to target to consider we 'reached' it, and stop driving.
//一个随机值的汽车基地徘徊(使AI汽车不都徘徊在同一模式)
private float m_RandomPerlin; // A random value for the car to base its wander on (so that AI cars don't all wander in the same pattern)
///参考我们正在控制的实际汽车控制器
private CarController m_CarController; // Reference to actual car controller we are controlling
///直到我们避开最近与之相撞的汽车为止。
private float m_AvoidOtherCarTime; // time until which to avoid the car we recently collided with
///由于与另一辆车相撞而减速的速度有多少?
private float m_AvoidOtherCarSlowdown; // how much to slow down due to colliding with another car, whilst avoiding
///方向(- 1或1),以抵消路径,以避免其他车辆,同时避免
private float m_AvoidPathOffset; // direction (-1 or 1) in which to offset path to avoid other car, whilst avoiding
private Rigidbody m_Rigidbody;
// private NavMeshAgent cube;
private void Awake()
{
// get the car controller reference
m_CarController = GetComponent<CarController>();
// give the random perlin a random value
m_RandomPerlin = Random.value*100;
m_Rigidbody = GetComponent<Rigidbody>();
}
// public Transform target;
// Use this for initialization
//void Start()
//{
// cube = gameObject.GetComponent<NavMeshAgent>();
//}
//// Update is called once per frame
//void Update()
//{
// cube.SetDestination(m_Target.position);
//}
private void FixedUpdate()
{
//foreach (Transform m_target in m_Target)
// {
//}
//如果没有目标或者没有行驶,不move,否则朝着目标行驶
if (m_Target == null || !m_Driving)
{
// Car should not be moving,
// use handbrake to stop
m_CarController.Move(0, 0, -1f, 1f);
}
else
{
Vector3 fwd = transform.forward;
//如果实际速度大于最大速度的1/10
if (m_Rigidbody.velocity.magnitude > m_CarController.MaxSpeed * 0.1f)
{
fwd = m_Rigidbody.velocity;
}
float desiredSpeed = m_CarController.MaxSpeed;
//现在该是决定我们是否应该放慢速度的时候了…
// now it's time to decide if we should be slowing down...
switch (m_BrakeCondition)//刹车条件
{
case BrakeCondition.TargetDirectionDifference:
{
//
// the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.
//检查我们的目标的角度相对于当前方向的汽车
// check out the angle of our target compared to the current direction of the car
float approachingCornerAngle = Vector3.Angle(m_Target.forward, fwd);
//也可以考虑我们当前的旋转量,相乘,然后以相同的方式比较作为即将到来的转向角度。
// also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
float spinningAngle = m_Rigidbody.angularVelocity.magnitude * m_CautiousAngularVelocityFactor;
//如果它与我们当前的角度不同,我们需要谨慎(也就是说减速)一定量。
// if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
float cautiousnessRequired = Mathf.InverseLerp(0, m_CautiousMaxAngle,
Mathf.Max(spinningAngle,
approachingCornerAngle));
desiredSpeed = Mathf.Lerp(m_CarController.MaxSpeed, m_CarController.MaxSpeed * m_CautiousSpeedFactor,
cautiousnessRequired);
break;
}
//目标距离
case BrakeCondition.TargetDistance:
{
// the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
// head for a stationary target and come to rest when it arrives there.
//计算汽车与目标间的距离
// check out the distance to target
Vector3 delta = m_Target.position - transform.position;
//反插值,计算两个值之间的Lerp参数。也就是value在from和to之间的比例值。
float distanceCautiousFactor = Mathf.InverseLerp(m_CautiousMaxDistance, 0, delta.magnitude);
//也考虑我们转向时的当前数目,相乘,然后。。
// also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
float spinningAngle = m_Rigidbody.angularVelocity.magnitude * m_CautiousAngularVelocityFactor;
// if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
float cautiousnessRequired = Mathf.Max(
Mathf.InverseLerp(0, m_CautiousMaxAngle, spinningAngle), distanceCautiousFactor);
//期望速度
desiredSpeed = Mathf.Lerp(m_CarController.MaxSpeed, m_CarController.MaxSpeed * m_CautiousSpeedFactor,
cautiousnessRequired);
break;
}
case BrakeCondition.NeverBrake:
break;
}
// 与其他车辆相撞引起的避让动作:
// Evasive action due to collision with other cars:
// our target position starts off as the 'real' target position
Vector3 offsetTargetPos = m_Target.position;
//如果采取避让动作去阻止卡在另一个车
// if are we currently taking evasive action to prevent being stuck against another car:
if (Time.time < m_AvoidOtherCarTime)
{
//减速有必要的话(如果碰撞发生时我们在另一辆车后面)
// slow down if necessary (if we were behind the other car when collision occured)
desiredSpeed *= m_AvoidOtherCarSlowdown;
//和转向的路径的目标是远离其他车侧
// and veer towards the side of our path-to-target that is away from the other car
offsetTargetPos += m_Target.right * m_AvoidPathOffset;
}
else
{
//无需采取回避行动,我们可以随意漫游到目标的路径,这可以帮助防止AI看起来过于均匀和机器在他们的驾驶。
// no need for evasive action, we can just wander across the path-to-target in a random way,
// which can help prevent AI from seeming too uniform and robotic in their driving
offsetTargetPos += m_Target.right *
(Mathf.PerlinNoise(Time.time * m_LateralWanderSpeed, m_RandomPerlin) * 2 - 1) *
m_LateralWanderDistance;
}
//使用不同的灵敏度取决于加速或制动:
// use different sensitivity depending on whether accelerating or braking:
float accelBrakeSensitivity = (desiredSpeed < m_CarController.CurrentSpeed)
? m_BrakeSensitivity
: m_AccelSensitivity;
//决定实际量的加速/制动输入以达到所需的速度。
// decide the actual amount of accel/brake input to achieve desired speed.
float accel = Mathf.Clamp((desiredSpeed - m_CarController.CurrentSpeed) * accelBrakeSensitivity, -1, 1);
// add acceleration 'wander', which also prevents AI from seeming too uniform and robotic in their driving
// i.e. increasing the accel wander amount can introduce jostling and bumps between AI cars in a race
accel *= (1 - m_AccelWanderAmount) +
(Mathf.PerlinNoise(Time.time * m_AccelWanderSpeed, m_RandomPerlin) * m_AccelWanderAmount);
// calculate the local-relative position of the target, to steer towards
Vector3 localTarget = transform.InverseTransformPoint(offsetTargetPos);
// work out the local angle towards the target
float targetAngle = Mathf.Atan2(localTarget.x, localTarget.z) * Mathf.Rad2Deg;
// get the amount of steering needed to aim the car towards the target
float steer = Mathf.Clamp(targetAngle * m_SteerSensitivity, -1, 1) * Mathf.Sign(m_CarController.CurrentSpeed);
// feed input to the car controller.
m_CarController.Move(steer, accel, accel, 0f);
// if appropriate, stop driving when we're close enough to the target.
if (m_StopWhenTargetReached && localTarget.magnitude < m_ReachTargetThreshold)
{
m_Driving = false;
}
}
}
//一个碰撞器或刚体触动另一个刚体或碰撞器,在每帧都会调用OnCollisionStay,直到它们之间离开不接触。
private void OnCollisionStay(Collision col)
{
//检测碰撞与其他车辆,以便我们可以采取规避行动。
// detect collision against other cars, so that we can take evasive action
if (col.rigidbody != null)
{
var otherAI = col.rigidbody.GetComponent<CarAIControl>();
if (otherAI != null)
{
// we'll take evasive action for 1 second
m_AvoidOtherCarTime = Time.time + 1;
// but who's in front?...
if (Vector3.Angle(transform.forward, otherAI.transform.position - transform.position) < 90)
{
// the other ai is in front, so it is only good manners that we ought to brake...
m_AvoidOtherCarSlowdown = 0.5f;
}
else
{
// we're in front! ain't slowing down for anybody...
m_AvoidOtherCarSlowdown = 1;
}
// 这两种汽车都应该采取避让行动,沿车道中心偏移,远离另一辆车。
// both cars should take evasive action by driving along an offset from the path centre,
// away from the other car
var otherCarLocalDelta = transform.InverseTransformPoint(otherAI.transform.position);
float otherCarAngle = Mathf.Atan2(otherCarLocalDelta.x, otherCarLocalDelta.z);
m_AvoidPathOffset = m_LateralWanderDistance * -Mathf.Sign(otherCarAngle);
}
}
}
public void SetTarget(Transform target)
{
m_Target = target;
m_Driving = true;
}
}
}
这篇博客主要探讨Unity的官方资源包CarAIControl,深入解析如何理解和应用该资源包进行AI车辆控制,帮助开发者了解相关AI控制技术。
597

被折叠的 条评论
为什么被折叠?



