AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
AC_Airplane_Wheel.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using Communicator;
5 using Commons;
6 namespace AirControl
7 {
11  public class AC_Airplane_Wheel : MonoBehaviour
12  {
13  #region Properties
14  public bool isGrounded = false;
15  public bool IsGrounded
16  {
17  get{ return isGrounded; }
18  }
19  #endregion
20 
21  #region Variables
22  [Header("Wheel Properties")]
23  public Transform wheelGraphic;
24  public bool isBraking=false;
25  private float brakePower;
26  public bool isSteering=false;
27  private float steerAngle;
28  public float motorTorque;
29 
30  private WheelCollider wheelCol;
31  private Vector3 worldPos;
32  private Quaternion worldRot;
33  private float slowlyBrake;
34  #endregion
35 
36  #region Builtin Methods
37  // Start is called before the first frame update
38  void Start()
39  {
40  brakePower = (float)CommonFunctions.airplanePreset[CommonFunctions.activeAirplane+"/brakePower"];
41  steerAngle = (float)CommonFunctions.airplanePreset[CommonFunctions.activeAirplane+"/steerAngle"];
42  wheelCol = GetComponent<WheelCollider>();
43  }
49  void Update(){
50  WheelHit hit;
51  if (wheelCol.GetGroundHit(out hit) && (StaticOutputSchema.IfCollision == false)){
52  string surface = hit.collider.tag;
53  float wheelPenalty = 100.0f;
54  if(hit.collider.tag != "Runway"){
55  StaticOutputSchema.Reward -= wheelPenalty;
56  StaticOutputSchema.IfCollision = true;
57  StaticOutputSchema.CollisionObject = surface;
58  }
59  }
60 
61  }
62 
63  // Update is called once per frame
64  #endregion
65 
66  #region Custom Method
67  public void initWheel(){
71 
72  if (wheelCol){
73  //setting wheel torque to really small, this way wheel dont exert ant force or get locked
74  wheelCol.motorTorque = 0.0000000001f;
75  }
76  }
82  {
83  if(wheelCol)
84  {
85 
86  wheelCol.GetWorldPose(out worldPos, out worldRot);
87  if(wheelGraphic)
88  { //updating wheel rotation
89  wheelGraphic.rotation = worldRot;
90  wheelGraphic.position = worldPos;
91  }// handle Else
92  if (isBraking)
93  {
94  if(input.Brake > 0.1f)
95  {
96  //slowly apply brake
97  slowlyBrake = Mathf.Lerp(slowlyBrake,input.Brake*brakePower, Time.deltaTime );
98  wheelCol.brakeTorque = slowlyBrake;
99  // Release motor torque to move forward
100  wheelCol.motorTorque = 0.0f;
101  }
102  else
103  {
104  //relase the brake
105  slowlyBrake = 0f;
106  wheelCol.brakeTorque = 0.0f;
107  // small motor torque not to allow airplane roll backward
108  wheelCol.motorTorque = motorTorque;
109  }
110  }
111  if(isSteering)
112  {
113  wheelCol.steerAngle = -input.Yaw * steerAngle;
114  }
115 
116  // check to see if the wheels are grounded
117  isGrounded = wheelCol.isGrounded;
118 
119  }
120  }
121  #endregion
122 
123  }
124 
125 }
AirControl.AC_BaseAirplane_Input
Base class to listen for keyboard Inputs
Definition: AC_BaseAirplane_Input.cs:12
AirControl.AC_Airplane_Wheel.initWheel
void initWheel()
Init wheel set the motor torque to very small nuber to allow it to roll freely
Definition: AC_Airplane_Wheel.cs:70
AirControl
Definition: AirplaneSelector.cs:8
Communicator
Definition: InputHandle.cs:10
AirControl.AC_Airplane_Wheel.HandleWheel
void HandleWheel(AC_BaseAirplane_Input input)
Handle whele graphics, and steering
Definition: AC_Airplane_Wheel.cs:81
AirControl.AC_Airplane_Wheel
Handle wheel braking and steering
Definition: AC_Airplane_Wheel.cs:11
Commons
Definition: AirplaneProperties.cs:14