private void FixedUpdate() { // Read input for the pitch, yaw, roll and throttle of the aeroplane. float roll = CrossPlatformInputManager.GetAxis("Mouse X"); float pitch = CrossPlatformInputManager.GetAxis("Mouse Y"); airBrakes = CrossPlatformInputManager.GetButton("Fire1"); yaw = CrossPlatformInputManager.GetAxis("Horizontal"); throttle = CrossPlatformInputManager.GetAxis("Vertical"); #if MOBILE_INPUT AdjustInputForMobileControls(ref roll, ref pitch, ref throttle); #endif // // Pass the input to the aeroplane aeroplane.Move(roll, pitch, yaw, throttle, airBrakes); }
private void FixedUpdate() { // Read input for the pitch, yaw, roll and throttle of the aeroplane. float roll = CrossPlatformInputManager.GetAxis("Horizontal"); float pitch = CrossPlatformInputManager.GetAxis("Vertical"); bool airBrakes = CrossPlatformInputManager.GetButton("Fire1"); // auto throttle up, or down if braking. float throttle = airBrakes ? -1 : 1; #if MOBILE_INPUT AdjustInputForMobileControls(ref roll, ref pitch, ref throttle); #endif // Pass the input to the aeroplane aeroplane.Move(roll, pitch, 0, throttle, airBrakes); }
// fixed update is called in time with the physics system update private void FixedUpdate() { if (m_Target != null) { // make the plane wander from the path, useful for making the AI seem more human, less robotic. /* * Vector3 targetPos = m_Target.position + * transform.right* * (Mathf.PerlinNoise(Time.time*m_LateralWanderSpeed, m_RandomPerlin)*2 - 1)* * m_LateralWanderDistance; */ Vector3 targetPos = m_Target.position; // adjust the yaw and pitch towards the target Vector3 localTarget = transform.InverseTransformPoint(targetPos); float targetAngleYaw = Mathf.Atan2(localTarget.x, localTarget.z); float targetAnglePitch = -Mathf.Atan2(localTarget.y, localTarget.z); // Set the target for the planes pitch, we check later that this has not passed the maximum threshold targetAnglePitch = Mathf.Clamp(targetAnglePitch, -m_MaxClimbAngle * Mathf.Deg2Rad, m_MaxClimbAngle * Mathf.Deg2Rad); // calculate the difference between current pitch and desired pitch float changePitch = targetAnglePitch - m_AeroplaneController.PitchAngle; // AI always applies gentle forward throttle const float throttleInput = 0.5f; // AI applies elevator control (pitch, rotation around x) to reach the target angle float pitchInput = changePitch * m_PitchSensitivity; // clamp the planes roll float desiredRoll = Mathf.Clamp(targetAngleYaw, -m_MaxRollAngle * Mathf.Deg2Rad, m_MaxRollAngle * Mathf.Deg2Rad); float yawInput = 0; float rollInput = 0; if (!m_TakenOff) { // If the planes altitude is above m_TakeoffHeight we class this as taken off if (m_AeroplaneController.Altitude > m_TakeoffHeight) { m_TakenOff = true; } } else { // now we have taken off to a safe height, we can use the rudder and ailerons to yaw and roll yawInput = targetAngleYaw; rollInput = -(m_AeroplaneController.RollAngle - desiredRoll) * m_RollSensitivity; } // adjust how fast the AI is changing the controls based on the speed. Faster speed = faster on the controls. float currentSpeedEffect = 1 + (m_AeroplaneController.ForwardSpeed * m_SpeedEffect); rollInput *= currentSpeedEffect; pitchInput *= currentSpeedEffect; yawInput *= currentSpeedEffect; // pass the current input to the plane (false = because AI never uses air brakes!) m_AeroplaneController.Move(rollInput, pitchInput, yawInput, throttleInput, false); } else { // no target set, send zeroed input to the planeW m_AeroplaneController.Move(0, 0, 0, 0, false); } }