Beispiel #1
0
    // Move the player, should only be used in FixUpdate()
    public void Move(float move, bool crouch, bool shouldJump, bool shouldCancelJump)
    {
        // Current velocity of player rigidbody
        Vector2 currentVelocity = characterController2d.GetVelocity();

        //only control the player if grounded or airControl is turned on
        if (isGrounded || m_AirControl)
        {
            // If crouching
            if (crouch)
            {
                // Reduce the speed by the crouchSpeed multiplier
                move *= m_CrouchSpeed;
            }

            // if not grounded
            if (!isGrounded && m_AirControl)
            {
                // Reduce the speed by the airborneSpeed multiplier
                move *= m_airborneSpeed;
            }

            UpdateFacing(move);

            // Move the character by finding the target velocity
            Vector3 targetVelocity = new Vector2(move * 10, currentVelocity.y);
            // And then smoothing it out and applying it to the character
            characterController2d.SetVelocity(Vector2.SmoothDamp(currentVelocity, targetVelocity, ref velocity, m_MovementSmoothing));
        }

        // If the player is pressing jump...
        if (shouldJump)
        {
            // Add a vertical speed to the player.
            isGrounded = false;
            characterController2d.AddVelocity(0.0f, m_JumpSpeed);
        }
        // If the player is releasing jumping...
        else if (shouldCancelJump)
        {
            // If player vertical speed is higher than m_JumpCancelSpeed, limit it ot m_JumpCancelSpeed
            if (currentVelocity.y > m_JumpCancelSpeed)
            {
                characterController2d.SetVeticalSpeed(m_JumpCancelSpeed);
            }
        }
    }