CarAIControl.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. using System;
  2. using UnityEngine;
  3. using Random = UnityEngine.Random;
  4. #pragma warning disable 649
  5. namespace UnityStandardAssets.Vehicles.Car
  6. {
  7. [RequireComponent(typeof (CarController))]
  8. public class CarAIControl : MonoBehaviour
  9. {
  10. public enum BrakeCondition
  11. {
  12. NeverBrake, // the car simply accelerates at full throttle all the time.
  13. TargetDirectionDifference, // the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.
  14. TargetDistance, // the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
  15. // head for a stationary target and come to rest when it arrives there.
  16. }
  17. // This script provides input to the car controller in the same way that the user control script does.
  18. // As such, it is really 'driving' the car, with no special physics or animation tricks to make the car behave properly.
  19. // "wandering" is used to give the cars a more human, less robotic feel. They can waver slightly
  20. // in speed and direction while driving towards their target.
  21. [SerializeField] [Range(0, 1)] private float m_CautiousSpeedFactor = 0.05f; // percentage of max speed to use when being maximally cautious
  22. [SerializeField] [Range(0, 180)] private float m_CautiousMaxAngle = 50f; // angle of approaching corner to treat as warranting maximum caution
  23. [SerializeField] private float m_CautiousMaxDistance = 100f; // distance at which distance-based cautiousness begins
  24. [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!)
  25. [SerializeField] private float m_SteerSensitivity = 0.05f; // how sensitively the AI uses steering input to turn to the desired direction
  26. [SerializeField] private float m_AccelSensitivity = 0.04f; // How sensitively the AI uses the accelerator to reach the current desired speed
  27. [SerializeField] private float m_BrakeSensitivity = 1f; // How sensitively the AI uses the brake to reach the current desired speed
  28. [SerializeField] private float m_LateralWanderDistance = 3f; // how far the car will wander laterally towards its target
  29. [SerializeField] private float m_LateralWanderSpeed = 0.1f; // how fast the lateral wandering will fluctuate
  30. [SerializeField] [Range(0, 1)] private float m_AccelWanderAmount = 0.1f; // how much the cars acceleration will wander
  31. [SerializeField] private float m_AccelWanderSpeed = 0.1f; // how fast the cars acceleration wandering will fluctuate
  32. [SerializeField] private BrakeCondition m_BrakeCondition = BrakeCondition.TargetDistance; // what should the AI consider when accelerating/braking?
  33. [SerializeField] private bool m_Driving; // whether the AI is currently actively driving or stopped.
  34. [SerializeField] private Transform m_Target; // 'target' the target object to aim for.
  35. [SerializeField] private bool m_StopWhenTargetReached; // should we stop driving when we reach the target?
  36. [SerializeField] private float m_ReachTargetThreshold = 2; // proximity to target to consider we 'reached' it, and stop driving.
  37. 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)
  38. private CarController m_CarController; // Reference to actual car controller we are controlling
  39. private float m_AvoidOtherCarTime; // time until which to avoid the car we recently collided with
  40. private float m_AvoidOtherCarSlowdown; // how much to slow down due to colliding with another car, whilst avoiding
  41. private float m_AvoidPathOffset; // direction (-1 or 1) in which to offset path to avoid other car, whilst avoiding
  42. private Rigidbody m_Rigidbody;
  43. private void Awake()
  44. {
  45. // get the car controller reference
  46. m_CarController = GetComponent<CarController>();
  47. // give the random perlin a random value
  48. m_RandomPerlin = Random.value*100;
  49. m_Rigidbody = GetComponent<Rigidbody>();
  50. }
  51. private void FixedUpdate()
  52. {
  53. if (m_Target == null || !m_Driving)
  54. {
  55. // Car should not be moving,
  56. // use handbrake to stop
  57. m_CarController.Move(0, 0, -1f, 1f);
  58. }
  59. else
  60. {
  61. Vector3 fwd = transform.forward;
  62. if (m_Rigidbody.velocity.magnitude > m_CarController.MaxSpeed*0.1f)
  63. {
  64. fwd = m_Rigidbody.velocity;
  65. }
  66. float desiredSpeed = m_CarController.MaxSpeed;
  67. // now it's time to decide if we should be slowing down...
  68. switch (m_BrakeCondition)
  69. {
  70. case BrakeCondition.TargetDirectionDifference:
  71. {
  72. // the car will brake according to the upcoming change in direction of the target. Useful for route-based AI, slowing for corners.
  73. // check out the angle of our target compared to the current direction of the car
  74. float approachingCornerAngle = Vector3.Angle(m_Target.forward, fwd);
  75. // also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
  76. float spinningAngle = m_Rigidbody.angularVelocity.magnitude*m_CautiousAngularVelocityFactor;
  77. // if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
  78. float cautiousnessRequired = Mathf.InverseLerp(0, m_CautiousMaxAngle,
  79. Mathf.Max(spinningAngle,
  80. approachingCornerAngle));
  81. desiredSpeed = Mathf.Lerp(m_CarController.MaxSpeed, m_CarController.MaxSpeed*m_CautiousSpeedFactor,
  82. cautiousnessRequired);
  83. break;
  84. }
  85. case BrakeCondition.TargetDistance:
  86. {
  87. // the car will brake as it approaches its target, regardless of the target's direction. Useful if you want the car to
  88. // head for a stationary target and come to rest when it arrives there.
  89. // check out the distance to target
  90. Vector3 delta = m_Target.position - transform.position;
  91. float distanceCautiousFactor = Mathf.InverseLerp(m_CautiousMaxDistance, 0, delta.magnitude);
  92. // also consider the current amount we're turning, multiplied up and then compared in the same way as an upcoming corner angle
  93. float spinningAngle = m_Rigidbody.angularVelocity.magnitude*m_CautiousAngularVelocityFactor;
  94. // if it's different to our current angle, we need to be cautious (i.e. slow down) a certain amount
  95. float cautiousnessRequired = Mathf.Max(
  96. Mathf.InverseLerp(0, m_CautiousMaxAngle, spinningAngle), distanceCautiousFactor);
  97. desiredSpeed = Mathf.Lerp(m_CarController.MaxSpeed, m_CarController.MaxSpeed*m_CautiousSpeedFactor,
  98. cautiousnessRequired);
  99. break;
  100. }
  101. case BrakeCondition.NeverBrake:
  102. break;
  103. }
  104. // Evasive action due to collision with other cars:
  105. // our target position starts off as the 'real' target position
  106. Vector3 offsetTargetPos = m_Target.position;
  107. // if are we currently taking evasive action to prevent being stuck against another car:
  108. if (Time.time < m_AvoidOtherCarTime)
  109. {
  110. // slow down if necessary (if we were behind the other car when collision occured)
  111. desiredSpeed *= m_AvoidOtherCarSlowdown;
  112. // and veer towards the side of our path-to-target that is away from the other car
  113. offsetTargetPos += m_Target.right*m_AvoidPathOffset;
  114. }
  115. else
  116. {
  117. // no need for evasive action, we can just wander across the path-to-target in a random way,
  118. // which can help prevent AI from seeming too uniform and robotic in their driving
  119. offsetTargetPos += m_Target.right*
  120. (Mathf.PerlinNoise(Time.time*m_LateralWanderSpeed, m_RandomPerlin)*2 - 1)*
  121. m_LateralWanderDistance;
  122. }
  123. // use different sensitivity depending on whether accelerating or braking:
  124. float accelBrakeSensitivity = (desiredSpeed < m_CarController.CurrentSpeed)
  125. ? m_BrakeSensitivity
  126. : m_AccelSensitivity;
  127. // decide the actual amount of accel/brake input to achieve desired speed.
  128. float accel = Mathf.Clamp((desiredSpeed - m_CarController.CurrentSpeed)*accelBrakeSensitivity, -1, 1);
  129. // add acceleration 'wander', which also prevents AI from seeming too uniform and robotic in their driving
  130. // i.e. increasing the accel wander amount can introduce jostling and bumps between AI cars in a race
  131. accel *= (1 - m_AccelWanderAmount) +
  132. (Mathf.PerlinNoise(Time.time*m_AccelWanderSpeed, m_RandomPerlin)*m_AccelWanderAmount);
  133. // calculate the local-relative position of the target, to steer towards
  134. Vector3 localTarget = transform.InverseTransformPoint(offsetTargetPos);
  135. // work out the local angle towards the target
  136. float targetAngle = Mathf.Atan2(localTarget.x, localTarget.z)*Mathf.Rad2Deg;
  137. // get the amount of steering needed to aim the car towards the target
  138. float steer = Mathf.Clamp(targetAngle*m_SteerSensitivity, -1, 1)*Mathf.Sign(m_CarController.CurrentSpeed);
  139. // feed input to the car controller.
  140. m_CarController.Move(steer, accel, accel, 0f);
  141. // if appropriate, stop driving when we're close enough to the target.
  142. if (m_StopWhenTargetReached && localTarget.magnitude < m_ReachTargetThreshold)
  143. {
  144. m_Driving = false;
  145. }
  146. }
  147. }
  148. private void OnCollisionStay(Collision col)
  149. {
  150. // detect collision against other cars, so that we can take evasive action
  151. if (col.rigidbody != null)
  152. {
  153. var otherAI = col.rigidbody.GetComponent<CarAIControl>();
  154. if (otherAI != null)
  155. {
  156. // we'll take evasive action for 1 second
  157. m_AvoidOtherCarTime = Time.time + 1;
  158. // but who's in front?...
  159. if (Vector3.Angle(transform.forward, otherAI.transform.position - transform.position) < 90)
  160. {
  161. // the other ai is in front, so it is only good manners that we ought to brake...
  162. m_AvoidOtherCarSlowdown = 0.5f;
  163. }
  164. else
  165. {
  166. // we're in front! ain't slowing down for anybody...
  167. m_AvoidOtherCarSlowdown = 1;
  168. }
  169. // both cars should take evasive action by driving along an offset from the path centre,
  170. // away from the other car
  171. var otherCarLocalDelta = transform.InverseTransformPoint(otherAI.transform.position);
  172. float otherCarAngle = Mathf.Atan2(otherCarLocalDelta.x, otherCarLocalDelta.z);
  173. m_AvoidPathOffset = m_LateralWanderDistance*-Mathf.Sign(otherCarAngle);
  174. }
  175. }
  176. }
  177. public void SetTarget(Transform target)
  178. {
  179. m_Target = target;
  180. m_Driving = true;
  181. }
  182. }
  183. }