private void FixedUpdate()
        {
            // Read input for the pitch, yaw, roll and throttle of the aeroplane.
            float roll  = CrossPlatformInputManager.GetAxis("Horizontal");
            float pitch = CrossPlatformInputManager.GetAxis("Mouse Y");

            m_AirBrakes = (CrossPlatformInputManager.GetButton("Fire1") || CrossPlatformInputManager.GetButton("Jump"));
            m_Yaw       = CrossPlatformInputManager.GetAxis("Mouse X");
            m_Throttle  = CrossPlatformInputManager.GetAxis("Vertical");
                        #if MOBILE_INPUT
            AdjustInputForMobileControls(ref roll, ref pitch, ref m_Throttle);
                        #endif
            // Pass the input to the aeroplane
            m_Aeroplane.Move(roll, pitch, m_Yaw, m_Throttle, m_AirBrakes);
        }
예제 #2
0
        // fixed update is called in time with the physics system update
        private void FixedUpdate()
        {
            if (m_Target != null)
            //	if( Vector3.Distance (m_Target.position, myTransform.position) < 40)
            {
                // 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;

                // 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);
            }
//			if( Vector3.Distance (Target2.position, myTransform.position) < 40)
//			{
//				Debug.Log("asteroide");
//				m_AeroplaneController.Move(0,0,0,0,false);
//			}
        }