Пример #1
0
        private float Roll_change  = 0.0f; // Z

        // Update Rotation Stuff (Returns Final Eulers)
        public void UpdateRotationEuler(ref Quadcopter.States State, ref Quadcopter.Settings Settings)
        {
            // Check Wind Direction
            if (WindGlobal.GetInstance() != null)
            {
                Vector3 WD = WindGlobal.GetInstance().GetWindDirection();
                float   WF = WindGlobal.GetInstance().GetWindStrength() * 0.1f;
                WF = Mathf.Pow(WF, 1.5f);


                if (WF != 0.0f)
                {
                    // If up is aiming perpendicular to wind, no effect
                    float WindEffect = Mathf.Abs(Vector3.Dot(WD, Quadcopter_Up)) * WF;

                    _Rigidbody.gameObject.transform.RotateAround(Position, Vector3.right, WD.y * WindEffect);
                    _Rigidbody.gameObject.transform.RotateAround(Position, Vector3.up, WD.x * WindEffect);
                    _Rigidbody.gameObject.transform.RotateAround(Position, Vector3.forward, WD.z * WindEffect);

                    //Vector3 TargetForward = WD;
                    //// Check if direction is closer to backwards
                    //if (Vector3.Distance(Quadcopter_Forward, WD) > Vector3.Distance(Quadcopter_Forward, -WD))
                    //{
                    //    //TargetForward = -WD;
                    //}
                    //
                    //// Slerp forward to target forward
                    //_Rigidbody.gameObject.transform.up = Vector3.Slerp(Quadcopter_Up, TargetForward, 0.02f * WindEffect).normalized;
                }
            }

            // Update movement rotation if not broken
            if (!Settings._Broken)
            {
                // Update Ideal Eulers
                Pitch_Change = State.GetEulerChanges().x *Settings._PitchSpeed *((Settings._InversePitch) ? -1.0f : 1.0f);
                Yaw_Change   = State.GetEulerChanges().y *Settings._YawSpeed *((Settings._InverseYaw) ? -1.0f : 1.0f);
                Roll_change  = State.GetEulerChanges().z *Settings._RollSpeed *((Settings._InverseRoll) ? -1.0f : 1.0f);

                // Update Rotations
                _Rigidbody.gameObject.transform.RotateAround(Position, Quadcopter_Right, Pitch_Change);
                _Rigidbody.gameObject.transform.RotateAround(Position, Quadcopter_Up, Yaw_Change);
                _Rigidbody.gameObject.transform.RotateAround(Position, Quadcopter_Forward, Roll_change);

                // If rest state, fix orientation if controls aren't being touced
                if (Pitch_Change == 0.0f && Yaw_Change == 0.0f && Roll_change == 0.0f && Settings._ThrusterRest)
                {
                    Vector3 Euler = _Rigidbody.gameObject.transform.eulerAngles;

                    float spd = 0.06f;
                    Euler.x = Mathfx.Clerp(Euler.x, 0.0f, spd);
                    //Euler.y = Mathfx.Clerp(Euler.y, 0.0f, spd);
                    Euler.z = Mathfx.Clerp(Euler.z, 0.0f, spd);

                    _Rigidbody.gameObject.transform.eulerAngles = Euler;
                }
            }
        }
Пример #2
0
        // Update Position
        public void UpdatePosition(ref Quadcopter.States State, ref Quadcopter.Settings Settings)
        {
            // Gravity
            Vector3 Gravity = new Vector3(0, Settings._Gravity * Settings._GravityModifier, 0);

            // Final Direction
            Vector3 FinalDirection = Gravity;

            // Float Thruster amount
            //float ThrusterAmount = State.GetThrusterUpwards();
            float ThrusterAmount = State.GetThrusters();

            // Inverse Setting
            if (Settings._InverseThrusters)
            {
                ThrusterAmount = ThrusterAmount * -1.0f;
            }

            // If broken, values are set to zero
            if (Settings._Broken)
            {
                ThrusterAmount = 0.0f;
                Speed_Ideal    = 0.0f;
            }
            // Normal thruster checks
            else
            {
                // Rotor Speed Ideal
                if (ThrusterAmount >= 0.0f)
                {
                    Speed_Ideal = ThrusterAmount * Settings._ThrusterSpeedUpwards * Settings._ThrusterSpeed;
                }
                else
                {
                    Speed_Ideal = ThrusterAmount * Settings._ThrusterSpeedDownwards * Settings._ThrusterSpeed;
                }

                // If thrusters are being used
                if (ThrusterAmount > 0.0f)
                {
                    // You move at least as the same speed as gravity
                    Speed_Current = Mathf.Max(Speed_Current, -Gravity.y * 0.88f);
                }
            }

            // Rest State if thruster is at zero and not broken
            bool RestSate = (ThrusterAmount == 0.0f && Settings._ThrusterRest == true) && !Settings._Broken;

            // Thruster rest
            if (RestSate)
            {
                // Lerp speed to rest
                Speed_Current = Mathf.Lerp(Speed_Current, -Gravity.y, 0.07f);

                // If close enough, snap to gravity speed
                if (Mathf.Abs(Speed_Current + Gravity.y) < 0.01f)
                {
                    Speed_Current = -Gravity.y;
                }
            }
            // Normal Transition
            else
            {
                // Lerp Speed to Ideal
                if (Speed_Current < Speed_Ideal)
                {
                    // Going Up
                    Speed_Current = Mathf.Lerp(Speed_Current, Speed_Ideal, Settings._ThrusterTransition_Upwards);
                }
                else
                {
                    // Going Down
                    Speed_Current = Mathf.Lerp(Speed_Current, Speed_Ideal, Settings._ThrusterTransition_Downwards);
                }
            }

            // Final Direction
            FinalDirection += Quadcopter_Up * Speed_Current;
            // If at rest, make sure y value clamps if close enough to zero
            if (RestSate && Mathf.Abs(FinalDirection.y) < 0.01f)
            {
                FinalDirection.y = 0.0f;
            }

            // Wind affects
            if (WindGlobal.GetInstance() != null)
            {
                // Wind affect velocity
                FinalDirection += (WindGlobal.GetInstance().GetWind());
            }

            // Set Velocity
            SetVelocity(FinalDirection * Settings._GlobalSpeedModifier);
        }