void Awake() { /* Create the player controller object in the code, linking the Unity * Engine input system to script functions */ controls = new PlayerControls(); /* context refers to any input data when a controlpad button is pressed, * providng input information which can be used */ controls.ControllerInput.RightThumbstick.performed += context => MoveUiJoystick(context.ReadValue <Vector2>()); controls.ControllerInput.RudderLeftDown.performed += context => ControlsUtilityMethods.PedalDownKeyboard("left"); controls.ControllerInput.RudderLeftUp.performed += context => ControlsUtilityMethods.PedalBothUp(); controls.ControllerInput.RudderRightDown.performed += context => ControlsUtilityMethods.PedalDownKeyboard("right"); controls.ControllerInput.RudderRightUp.performed += context => ControlsUtilityMethods.PedalBothUp(); controls.ControllerInput.FlapsDown.performed += context => ControlsUtilityMethods.MoveFlapsDown(); controls.ControllerInput.FlapsUp.performed += context => ControlsUtilityMethods.MoveFlapsUp(); controls.ControllerInput.LeftThumbstick.performed += context => ControlsUtilityMethods.MoveUiThrottle(context.ReadValue <Vector2>()); }
// Aircraft Methods /* Method updates the rotation speed and aquires current control surface * values for each axis (x,y,z) */ public void RotateAxis() { // Calculate rotation speed based on UI Slider value rotationSpeed = RotationSpeedCalc(); // Get all of the control surfaces relevant angles normalised to // values between 180/-180 float rudderInspectorFloat = ControlsUtilityMethods.WrapAngle(this.rudder.GetCurrentRotations().z); float aileronInspectorFloat = ControlsUtilityMethods.WrapAngle(this.rightAileron.GetCurrentRotations().y); float elevatorInspectorFloat = ControlsUtilityMethods.WrapAngle(this.rightElevator.GetCurrentRotations().y); // Map variables to the corresponding control surfaces float roll = aileronInspectorFloat; // ailerons (x axis) float pitch = elevatorInspectorFloat; // elevators (y axis) float yaw = rudderInspectorFloat; // rudder (z axis) // AxisRotate method rotates the aircraft on each axis individually // in the correct order AxisRotate(pitch, Vector3.up, Vector3.down); // y axis movement rotating either up or down AxisRotate(roll, Vector3.right, Vector3.left); // x axis movement rotating either right or left AxisRotate(yaw, Vector3.back, Vector3.forward); // z axis movment, rotating either back or forward }
/* Update is called once per frame Each method is called from the * ControlsUtilityMethods.cs script as that script contains all of the cockpit * control postions Update method controls how the surfaces are moved based on * there current rotation values, changed by user input on the cockpit controls * Cloud speed is also controlled per frame here */ void Update() { // Rotate surfaces based on UI Joystick location ControlsUtilityMethods.RotateSurfaces(); // Rotate flaps based on UI Flap Slider position ControlsUtilityMethods.MoveFlaps(); // Update the cloud speed based on throttle position ControlsUtilityMethods.UpdateCloudSpeed(); // Rotate the aircraft depending on the control surface defelcted // positions aircraft.RotateAxis(); // Rotate cloud pivot based on aircrafts Z axis (yaw) aircraft.RotateClouds(); }
/* Update is called once per frame Each frame the update method is used to * determine the current position of each control surface, updating the text * box with the correct user information */ void Update() { // update the position value rudderZAngle = ControlsUtilityMethods.WrapAngle(rudder.GetCurrentRotations().z); // Generate a text string for the control surface based on position GenerateTextDescription("rudder", rudderZAngle); aileronYAngle = ControlsUtilityMethods.WrapAngle(leftAileron.GetCurrentRotations().y); GenerateTextDescription("ailerons", aileronYAngle); elevatorYAngle = ControlsUtilityMethods.WrapAngle(leftElevator.GetCurrentRotations().y); GenerateTextDescription("elevators", elevatorYAngle); // Method is called to check if another UI component is covering the text box TextOutput(); }
// Awake method called when game loads void Awake() { /* Get a reference to the Unity engine Player Controls. * Unity Input system maps performed function to key press */ controls = new PlayerControls(); // Each keyboard input is mapped to a function call Rudder Keys controls.KeyboardInput.RudderLeftDown.performed += context => ControlsUtilityMethods.PedalDownKeyboard("left"); controls.KeyboardInput.RudderLeftUp.performed += context => ControlsUtilityMethods.PedalBothUp(); controls.KeyboardInput.RudderRightDown.performed += context => ControlsUtilityMethods.PedalDownKeyboard("right"); controls.KeyboardInput.RudderRightUp.performed += context => ControlsUtilityMethods.PedalBothUp(); // Elevator Keys controls.KeyboardInput.ElevatorsUp.performed += context => ButtonJoystickMoveElevators("down"); controls.KeyboardInput.ElevatorsDown.performed += context => ButtonJoystickMoveElevators("up"); controls.KeyboardInput.ElevatorsUpReverse.performed += context => ButtonJoystickMoveElevators("reverse"); controls.KeyboardInput.ElevatorsDownReverse.performed += context => ButtonJoystickMoveElevators("reverse"); // Aileron Keys controls.KeyboardInput.AileronsUp.performed += context => ButtonJoystickMoveAilerons("up"); controls.KeyboardInput.AileronsDown.performed += context => ButtonJoystickMoveAilerons("down"); controls.KeyboardInput.AileronsUpReverse.performed += context => ButtonJoystickMoveAilerons("reverse"); controls.KeyboardInput.AileronsDownReverse.performed += context => ButtonJoystickMoveAilerons("reverse"); // Flaps Keys controls.KeyboardInput.FlapsDown.performed += context => ControlsUtilityMethods.MoveFlapsDown(); controls.KeyboardInput.FlapsUp.performed += context => ControlsUtilityMethods.MoveFlapsUp(); // Throttle Keys controls.KeyboardInput.ThrottleDown.performed += context => MoveThrottle("down"); controls.KeyboardInput.ThrottleUp.performed += context => MoveThrottle("up"); }
// Rotate clouds based on aircrafts Z rotation movement, so they are // always moving towards aircraft public void RotateClouds() { float rudderInspectorFloat = ControlsUtilityMethods.WrapAngle(this.rudder.GetCurrentRotations().z); // current aircraft Z rotation if (rudderInspectorFloat >= 1) // Rotate the clouds in conjunction with the rudder, so they follow // the aircrafts rotation { this.cloudPivot.transform.localRotation *= Quaternion.AngleAxis((rudderInspectorFloat) * rotationSpeed * Time.deltaTime, Vector3.back); } else if (rudderInspectorFloat < 0) { this.cloudPivot.transform.localRotation *= Quaternion.AngleAxis((-rudderInspectorFloat) * rotationSpeed * Time.deltaTime, Vector3.forward); } }
// Start is called before the first frame update void Start() { // Add references to the control surface objects rudder = ControlSurfaces.rudder; leftAileron = ControlSurfaces.leftAileron; leftElevator = ControlSurfaces.leftElevator; // Get the relevant start angles for each surface rudderZAngle = ControlsUtilityMethods.WrapAngle(rudder.GetCurrentRotations().z); aileronYAngle = ControlsUtilityMethods.WrapAngle(leftAileron.GetCurrentRotations().y); elevatorYAngle = ControlsUtilityMethods.WrapAngle(leftElevator.GetCurrentRotations().y); /* Get the TextMeshPro via code. This text area is where description * text is shown to the user */ var textArray = FindObjectsOfType <TextMeshProUGUI>(); foreach (var element in textArray) // loop through all text mesh pro objects { if (element.tag == "ControlsDescription") { /* If the object is tagged as "ControlsDescription" the reference is stored * in the variable allowing us to print directly into the text box through code */ controlSurfaceDescriptions = element; } } // Get the UI text box background, enabling us to show and hide it controlInputDescriptionBackground = GameObject.Find("ControlsDescriptionBackground").GetComponent <Image>(); // Store initial values of MenuSystem script booleans infoIsVisible = MenuSystem.infoIsVisible; controlsIsVisible = MenuSystem.controlsIsVisible; }
// When mouse no longer clicking, move both pedals back up public void OnPointerUp() { ControlsUtilityMethods.PedalBothUp(); }
// Move right pedal down and left pedal up - movement controlled by PedalDown method public void OnRightPedalDown() { ControlsUtilityMethods.PedalDown(rightPedal, MOUSE_DEGREES); ControlsUtilityMethods.PedalUp(leftPedal); }