using System.Collections; using System.Collections.Generic; using UnityEngine; public class PhysicsMaster : MonoBehaviour { public Rigidbody rb; public double Velocity; public double Altitude; public double Thrust; public double Fuel; public double Weight; public double Momentum; public double DynamicPressure; public double AtmosphericDensity; public double FlowVelocity; public double Drag; public double DragCoefficient; public double CrossSectionalArea; public double Lift; public double LiftCoefficient; public double LiftingArea; public double StabilityForce; public double StabilityCoefficient; public double AngleOfAttack; public double SideAngleOfAttack; public double Authority; public double Timer; public GameMaster gameMaster; public EndScreen endScreen; public InventoryMaster inventoryMaster; int FrameIntervalCounter; // Start is called before the first frame update void Start() { rb.isKinematic = true; Thrust = 70000; DragCoefficient = 0.002; CrossSectionalArea = 0.80; LiftingArea = 2.092700; Authority = 400; Timer = 0; } // Update is called once per frame void FixedUpdate() { rb.mass = Instantiator.Mass; Weight = rb.mass * 9.81; if (gameMaster.GameState == "Flying") { rb.isKinematic = false; //Inherent Parameters dictated by parts purchased InventoryMaster.BodyData.ExportStat(); InventoryMaster.Stage1Data.ExportStat(); //Calculating Dynamic Parameters Velocity = rb.velocity.magnitude; Altitude = transform.position.y; AtmosphericDensity = 1.224 - 0.000109 * Altitude; FlowVelocity = Velocity; DynamicPressure = AtmosphericDensity * FlowVelocity * FlowVelocity / 2; Drag = DragCoefficient * DynamicPressure * CrossSectionalArea; rb.drag = Time.deltaTime * (float)Drag; LiftCoefficient = 2.0 * Mathf.PI * AngleOfAttack; Lift = LiftCoefficient * DynamicPressure * LiftingArea; StabilityForce = 2.0 * Mathf.PI * SideAngleOfAttack * DynamicPressure * LiftingArea * 0.35; Momentum = rb.mass * Velocity; //Calculating Angle of Attack AngleOfAttack = Mathf.PI / 180 * Vector3.SignedAngle(transform.up, Vector3.ProjectOnPlane(rb.velocity, transform.right), transform.right); Debug.DrawRay(transform.position, 10 * rb.velocity, Color.blue, 0.1f, false); SideAngleOfAttack = Mathf.PI / 180 * Vector3.SignedAngle(transform.up, Vector3.ProjectOnPlane(rb.velocity, transform.forward), transform.forward); //Calculated Forces rb.AddRelativeForce(0, 0, -(float)Lift); rb.AddRelativeForce((float)StabilityForce, 0, 0); rb.AddForce(0, -(float)Weight, 0); Timer = Timer + Time.deltaTime; } //Control Blocks if (gameMaster.GameState == "Flying") { if (Input.GetKey("z")) { rb.AddRelativeForce(0, (float)Thrust, 0); } if (Input.GetKey("w")) { rb.AddRelativeTorque((float)Authority, 0, 0); } if (Input.GetKey("a")) { rb.AddRelativeTorque(0, 0, (float)Authority * (float)0.3); } if (Input.GetKey("s")) { rb.AddRelativeTorque(-(float)Authority, 0, 0); } if (Input.GetKey("d")) { rb.AddRelativeTorque(0, 0, -(float)Authority * (float)0.3); } if (Input.GetKey("q")) { rb.AddRelativeTorque(0, (float)Authority, 0); } if (Input.GetKey("e")) { rb.AddRelativeTorque(0, -(float)Authority, 0); } //Block to end flight if (Velocity < 0.01 && Altitude < 2) { gameMaster.GameState = "EndScreen"; endScreen.DayEnd(); rb.isKinematic = true; Timer = 0; } } } }