Esempio n. 1
0
    public void Move(float move, bool crouch, bool jump, float jumpTakeOffDirection, EJumpPhase jumpPhase)
    {
        // If the player should jump...
        if (jump && CanJump())
        {
            // Add a force to the player according to his direction.
            m_LastJumpTakeOffTimeStamp = Time.time;

            StopMovement();

            GetJumpAngleAndForce(move, jumpTakeOffDirection, out float jumpAngleInDegree, out float jumpForce);
            Vector2 jumpForceDirection = GetJumpForceDirection(jumpAngleInDegree, jumpForce);
            m_Rigidbody2D.AddForce(jumpForceDirection, ForceMode2D.Impulse);

            m_CharacterIsJumping = true;
        }

        if (CanMove())
        {
            if (m_Grounded && !m_CharacterIsJumping)
            {
                // If crouching
                if (crouch)
                {
                    if (!m_wasCrouching)
                    {
                        m_wasCrouching = true;
                    }

                    // Reduce the speed by the crouchSpeed multiplier
                    move *= m_ControllerConfig.m_CrouchSpeed;
                }
                else
                {
                    if (m_wasCrouching)
                    {
                        m_wasCrouching = false;
                    }
                }
            }

            // Move the character by finding the target velocity
            Vector2 targetVelocity = new Vector2(move * GetWalkSpeed(move, jumpPhase) * 10.0f, m_Rigidbody2D.velocity.y);
            // And then smoothing it out and applying it to the character
            m_Rigidbody2D.velocity = Vector2.SmoothDamp(m_Rigidbody2D.velocity, targetVelocity, ref m_Velocity, m_ControllerConfig.m_MovementSmoothing);

            // If the input is moving the player right and the player is facing left...
            if (move > 0f && !m_MovingRight)
            {
                OnDirectionChanged();
            }
            // Otherwise if the input is moving the player left and the player is facing right...
            else if (move < 0f && m_MovingRight)
            {
                OnDirectionChanged();
            }
        }
    }
Esempio n. 2
0
    public void ChangePlayerStance(EPlayerStance newStance, EJumpPhase newJumpPhase)
    {
        if (m_PlayerStance != newStance)
        {
            ChronicleManager.AddChronicle(gameObject, EChronicleCategory.Movement, "On player stance changed | " + string.Format("{0,-20} {1,-20}", ("Old stance : " + m_PlayerStance), "New stance : " + newStance));
            m_PlayerStance = newStance;
            m_Animator.ResetTrigger(K_ANIM_TURN_AROUND_TRIGGER);
        }

        if (m_JumpPhase != newJumpPhase)
        {
            ChronicleManager.AddChronicle(gameObject, EChronicleCategory.Movement, "On player jump phase changed | " + string.Format("{0,-20} {1,-20}", ("Old phase : " + m_JumpPhase), "New phase : " + newJumpPhase));
            m_JumpPhase = newJumpPhase;
        }
    }
Esempio n. 3
0
    private float GetWalkSpeed(float move, EJumpPhase jumpPhase)
    {
        if (jumpPhase != EJumpPhase.TakeOff && jumpPhase != EJumpPhase.Landing)
        {
            if (move > 0f)
            {
                return((m_FacingRight) ? m_ControllerConfig.m_WalkForwardSpeed : m_ControllerConfig.m_WalkBackwardSpeed);
            }
            else if (move < 0f)
            {
                return((m_FacingRight) ? m_ControllerConfig.m_WalkBackwardSpeed : m_ControllerConfig.m_WalkForwardSpeed);
            }
        }

        return(0f);
    }