예제 #1
0
    private void FixedUpdate()
    {
        if (m_WantsToJump)
        {
            Rigidbody.AddForce(Vector3.up * m_JumpForce);
            m_WantsToJump = false;
        }

        if (m_WantsToJetpack && gameManager.GetAmmo() > 0)
        {
            jetPack.SpawnMinions();
            gameManager.DecreaseAmmo(jetPack.AmmoPerSecond * Time.deltaTime);
            Rigidbody.AddForce(Vector3.up * jetPack.Strength);
        }

        Rigidbody.rotation = Quaternion.Slerp(Rigidbody.rotation, Quaternion.Euler(0.0f, m_TargetRotation, 0.0f), 15.0f * Time.deltaTime);
        Rigidbody.AddForce(m_CurrentVelocity * Time.deltaTime, ForceMode.VelocityChange);

        for (int i = 0; i < externalForces.Count; i++)
        {
            if (externalForces[i].resetVelocity)
            {
                Vector3 v = Rigidbody.velocity;
                if (externalForces[i].resetVelocityDirection.x != 0)
                {
                    v.x = 0.0f;
                }
                if (externalForces[i].resetVelocityDirection.y != 0)
                {
                    v.y = 0.0f;
                }
                if (externalForces[i].resetVelocityDirection.z != 0)
                {
                    v.z = 0.0f;
                }
                Rigidbody.velocity = v;
            }

            if (externalForces[i].scale && IsGrounded)
            {
                Rigidbody.AddForce(externalForces[i].force * 200.0f, externalForces[i].mode);
            }
            else
            {
                Rigidbody.AddForce(externalForces[i].force, externalForces[i].mode);
            }
        }

        float   friction           = IsGrounded ? m_GroundFriction : m_AirFriction;
        Vector2 horizontalVelocity = new Vector3(Rigidbody.velocity.x, Rigidbody.velocity.z);

        if (friction == 0.0f)
        {
            friction = 1.0f;
        }

        Vector3 vel = transform.InverseTransformDirection(Rigidbody.velocity);

        vel.x *= friction;
        vel.z *= friction;

        if (!IsGrounded)
        {
            if (Rigidbody.velocity.y <= 0.0f)
            {
                if (m_WantsToJetpack)
                {
                    vel.y *= 0.99f;
                }
                else
                {
                    // vel.y *= m_ExtraGravityFactor;
                }
            }
        }

        vel.y = Mathf.Sign(vel.y) * Mathf.Min(Mathf.Abs(vel.y), m_MaxGravityVelocity);

        Vector2 hMov = new Vector2(vel.x, vel.z);

        hMov  = hMov.normalized * Mathf.Min(hMov.magnitude, m_MaxMovementVelocity);
        vel.x = hMov.x;
        vel.z = hMov.y;

        Rigidbody.velocity = transform.TransformDirection(vel);

        thirdPersonCamera.ManualUpdate();

        externalForces.Clear();
    }