AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
AC_Airplane_GroundEffect.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using Commons;
5 namespace AirControl
6 {
7  public class AC_Airplane_GroundEffect : MonoBehaviour
8  {
9  // Start is called before the first frame update
10  #region Variables
11  [Tooltip("Distance from ground when ground effect ends")]
12  private float groundDistance;
13  [Tooltip("Max list force")]
14  private float liftForce;
15  [Tooltip("Max speed for max round effect")]
16  private float maxSpeed;
17 
18  private Rigidbody rb;
19  #endregion
20 
21  #region Builtin Methods
22  // Start is called before the first frame update
23  void Start()
24  {
25  groundDistance = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+"/groundDistance"];
26  liftForce = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+"/liftForce"];
27  maxSpeed = (float)CommonFunctions.airplanePreset[CommonFunctions.ActiveAirplane+"/maxSpeed"];
28  rb = GetComponent<Rigidbody>();
29  }
30 
31  // Update is called once per frame
32  void FixedUpdate()
33  {
34  if(rb)
35  {
37  }
38  }
39  #endregion
40 
41  #region Custom Methods
42  protected virtual void HandleGroundEffect()
46  {
47  RaycastHit hit;
48  if(Physics.Raycast(transform.position, Vector3.down, out hit))
49  {
50  if(hit.distance < groundDistance && hit.transform.tag=="Ground")
51  {
52 
53  float currentSpeed = rb.velocity.magnitude;
54  float normalizedSpeed = currentSpeed/maxSpeed;
55  normalizedSpeed = Mathf.Clamp01(normalizedSpeed);
56 
57  float distance = groundDistance - hit.distance;
58  float finalForce = liftForce *distance *normalizedSpeed;
59  rb.AddForce(Vector3.up*finalForce);
60  }
61  }
62 
63  }
64  #endregion
65  }
66 
67 }
AirControl.AC_Airplane_GroundEffect
Definition: AC_Airplane_GroundEffect.cs:7
AirControl.AC_Airplane_GroundEffect.HandleGroundEffect
virtual void HandleGroundEffect()
Creates the Ground effect - https://en.wikipedia.org/wiki/Ground_effect_(aerodynamics)
Definition: AC_Airplane_GroundEffect.cs:45
AirControl
Definition: AirplaneSelector.cs:8
Commons
Definition: AirplaneProperties.cs:14