Advertisement
Muk99

VehicleAI

Jul 5th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.88 KB | None | 0 0
  1. using UnityEngine;
  2.  
  3. public class VehiclesAI : MonoBehaviour {
  4.  
  5.     #region InspectorField
  6.     public VehiclesController vehicle;
  7.     public float recalculateDelay = 0.75f, angularBrakingSpeed = 20;
  8.     #endregion
  9.  
  10.     #region PublicField
  11.     [HideInInspector]
  12.     public Vector3 target;
  13.     #endregion
  14.  
  15.     #region PrivateField
  16.     private NavMeshPath path;
  17.     private float acceleration, steering, steeringAngle, angle, handBraking;
  18.     private bool possiblePath, pointBehind;
  19.  
  20.     private Transform carTransform;
  21.     //private Transform carTransform {
  22.     //    get {
  23.     //        return vehicle.transform;
  24.     //    }
  25.     //}
  26.     private Vector3 carPos {
  27.         get {
  28.             return carTransform.position;
  29.         }
  30.     }
  31.     private Vector3 carFwr {
  32.         get {
  33.             return carTransform.forward;
  34.         }
  35.     }
  36.     private Vector3 currentPoint {
  37.         get {
  38.             if (path.corners.Length > 1)
  39.                 return path.corners[1];
  40.             else
  41.                 return carPos;
  42.         }
  43.     }
  44.     private Vector3 currentPointDirection {
  45.         get {
  46.             return currentPoint - carPos;
  47.         }
  48.     }
  49.     private float currentPointDistance {
  50.         get {
  51.             return currentPointDirection.magnitude;
  52.         }
  53.     }
  54.     private bool completedPath {
  55.         get {
  56.             return currentPointDistance < 3f && path.corners.Length <= 2;
  57.         }
  58.     }
  59.     private bool nearPoint {
  60.         get {
  61.             return vehicle.velocity > currentPointDistance + angularBrakingSpeed;
  62.         }
  63.     }
  64.     #endregion
  65.  
  66.     #region PrivateMethods
  67.     void Start() {
  68.         SetIATransform();
  69.  
  70.         foreach (var w in vehicle.wheels)
  71.             if (w.maxSteeringAngle > steeringAngle)
  72.                 steeringAngle = w.maxSteeringAngle;
  73.  
  74.         if (steeringAngle == 0) {
  75.             steeringAngle = 1;
  76.             Debug.LogError("This vehicle don't steer");
  77.         }
  78.  
  79.         path = new NavMeshPath();
  80.         InvokeRepeating("CalculatePath", 0f, recalculateDelay);
  81.     }
  82.  
  83.     void Update() {
  84.         StabilizePathHeight();
  85.         CalculateAngle();
  86.         ControlVehicle();
  87.     }
  88.  
  89.     void SetIATransform() {
  90.         carTransform = (Instantiate(new GameObject()) as GameObject).transform;
  91.         carTransform.parent = vehicle.transform;
  92.         carTransform.name = "IAPoint";
  93.         carTransform.localPosition = Vector3.forward * vehicle.GetComponentInChildren<MeshCollider>().bounds.extents.z;
  94.         carTransform.localRotation = Quaternion.identity;
  95.  
  96.     }
  97.  
  98.     void ControlVehicle() {
  99.         pointBehind = carTransform.InverseTransformPoint(currentPoint).z < 0;
  100.  
  101.         CalculateSteering();
  102.         CalculateAcceleration();
  103.         CalculateHandBraking();
  104.  
  105.         vehicle.inputs.vertical = acceleration;
  106.         vehicle.inputs.horizontal = steering;
  107.         vehicle.inputs.handbraking = handBraking;
  108.     }
  109.  
  110.     void CalculateAcceleration() {
  111.         if (!possiblePath || completedPath) {
  112.             acceleration = 0;
  113.             return;
  114.         }
  115.  
  116.         acceleration = Mathf.Clamp(1 - Mathf.Abs(steering), .5f, 1f);
  117.  
  118.         if (nearPoint)
  119.             acceleration = -Mathf.Sign(acceleration);
  120.  
  121.         else if (pointBehind) {
  122.             if (vehicle.velocity > -10)
  123.                 acceleration = -1f;
  124.             else
  125.                 acceleration *= -1f;
  126.         }
  127.  
  128.         else if (vehicle.velocity < 10f)
  129.             acceleration = 1f;
  130.  
  131.         acceleration = Mathf.Clamp(acceleration, -1, 1);
  132.     }
  133.  
  134.     void CalculateSteering() {
  135.         //steering = Mathf.Clamp(angle / steeringAngle, -1, 1);
  136.  
  137.         steering = angle / steeringAngle;
  138.  
  139.         steering *= vehicle.engine.reversing ? -1f : 1f;
  140.  
  141.         steering = Mathf.Clamp(steering, -1, 1);
  142.     }
  143.  
  144.     void CalculateHandBraking() {
  145.         handBraking = !possiblePath || completedPath ? 1f : 0f;
  146.     }
  147.  
  148.     void CalculatePath() {
  149.         possiblePath = NavMesh.CalculatePath(carPos, target, NavMesh.AllAreas, path);
  150.         StabilizePathHeight();
  151.     }
  152.  
  153.     void CalculateAngle() {
  154.         if (path.corners.Length > 1)
  155.             angle = Vector3.Angle(carFwr, currentPointDirection);
  156.         else
  157.             angle = 0;
  158.  
  159.         if (Vector3.Cross(carFwr, currentPointDirection).y < 0)
  160.             angle *=-1;
  161.     }
  162.  
  163.     void StabilizePathHeight() {
  164.         for (int c = 0; c < path.corners.Length; c++)
  165.             path.corners[c].y = carPos.y;
  166.     }
  167.  
  168. #if UNITY_EDITOR
  169.     void OnDrawGizmos() {
  170.         if (UnityEditor.EditorApplication.isPlaying && isActiveAndEnabled) {
  171.             for (int i = 0; i < path.corners.Length - 1; i++)
  172.                 if (i + 1 <= path.corners.Length)
  173.                     Gizmos.DrawLine(path.corners[i], path.corners[i + 1]);
  174.  
  175.             Gizmos.color = Color.black;
  176.             Gizmos.DrawRay(carPos, currentPointDirection);
  177.         }
  178.     }
  179. #endif
  180.     #endregion
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement