AirControl  1.3.0
Open Source, Modular, and Extensible Flight Simulator For Deep Learning Research
AC_Basic_Follow_Camera.cs
1 using System.Collections;
2 using System.Collections.Generic;
3 using UnityEngine;
4 using Commons;
5 using System.Dynamic;
6 using System.ComponentModel;
7 
8 namespace AirControl
9 {
13  public class AC_Basic_Follow_Camera : MonoBehaviour
14  {
15  #region Variables
16  [Header("Basic follow camera properties")]
17  [Tooltip("Drag and drop entire airplane group over here")]
18  public Transform airplane;
19  private float cameraDistance;
20  private float cameraHeight; //AirplaneProperties.getFloat(CommonFunctions.ActiveAirplane, "cameraHeight");
21  private float cameraMovementSpeed = 0.5f;
22  private float minHeaightFromGround;
23 
24  private Vector3 smoothVelocity;
25  protected float originalCamraHeight;
26  #endregion
27 
28  #region Builtin Methods
29  // Start is called before the first frame update
30 
31  void Start()
32  {
33  cameraDistance = (float)CommonFunctions.airplanePreset[CommonFunctions.activeAirplane+"/cameraDistance"];
34  cameraHeight = (float)CommonFunctions.airplanePreset[CommonFunctions.activeAirplane+"/cameraHeight"]; //AirplaneProperties.getFloat(CommonFunctions.ActiveAirplane, "cameraHeight");
35  cameraMovementSpeed = 0.5f;
36  minHeaightFromGround = (float)CommonFunctions.airplanePreset[CommonFunctions.activeAirplane+"/minHeaightFromGround"];
37  airplane = GameObject.Find(CommonFunctions.ActiveAirplane).GetComponent<Transform>();
38  originalCamraHeight = cameraHeight;
39  }
40 
41  // Update is called once per frame
42  void FixedUpdate()
43  {
44  if(airplane){
45  HandleCamera();
46  }// handel else
47 
48  }
49  #endregion
50 
51  #region Custom Methods
52  protected virtual void HandleCamera()
56  {
57  // Camera that follow the target
58  // -airplane.forward*cameraDistance Negative to bring the camera back of the airplane
59  Vector3 wantedPosition = airplane.position + (-airplane.forward*cameraDistance) + Vector3.up*cameraHeight;
60  // Debug.DrawLine(airplane.position, wantedPosition, Color.blue);
61  transform.position = Vector3.SmoothDamp(transform.position, wantedPosition, ref smoothVelocity, cameraMovementSpeed);
62  // Watch the airplane
63  // transform.LookAt(airplane.transform.position, Vector3.up);
64  RaycastHit hit;
65  if(Physics.Raycast(transform.position, Vector3.down, out hit)){
66  if(hit.distance < minHeaightFromGround && hit.transform.tag == "Ground"){
67  float wantedHeight = originalCamraHeight + (minHeaightFromGround - hit.distance);
68  cameraHeight = wantedHeight;
69  }
70  }
71  }
72  #endregion
73  }
74 
75 
76 }
77 
AirControl.AC_Basic_Follow_Camera.HandleCamera
virtual void HandleCamera()
Setup follow camera height and distance from the Airplane
Definition: AC_Basic_Follow_Camera.cs:55
AirControl.AC_Basic_Follow_Camera
Setup follow camera
Definition: AC_Basic_Follow_Camera.cs:13
AirControl
Definition: AirplaneSelector.cs:8
Commons
Definition: AirplaneProperties.cs:14