Пример #1
0
 private void Awake()
 {
     myVelocity = Vector2.zero;
     // Setting up references.
     priv_Anim           = GetComponent <Animator>();
     priv_Rigidbody2D    = GetComponent <Rigidbody2D>();
     quickCapsule        = GetComponent <CapsuleCollider2D>();
     crouchCheckPosition = new Vector3(
         quickCapsule.offset.x * Mathf.Abs(transform.lossyScale.x),
         (quickCapsule.offset.y + quickCapsule.size.y * 0.0125f) * transform.lossyScale.y,
         transform.position.z);
     crouchCheckSize = new Vector2(
         quickCapsule.size.x * 0.95f * Mathf.Abs(transform.lossyScale.x),
         quickCapsule.size.y * 0.975f * transform.lossyScale.y);
     crouchCheckDirection = quickCapsule.direction;
     //not necessary, but initializing the enums here to make myself feel better
     stateGrounded      = PlayerStateGrounded.Idle;
     stateAerial        = PlayerStateAerial.No;
     stateCrouch        = PlayerStateCrouch.No;
     myOriginalMaxSpeed = myMaxSpeed;
 }
Пример #2
0
        public void Move(float move, bool crouch, bool jump, bool jumpHold)
        {
            CollisionChecks();
            if (priv_Grounded && Physics2D.OverlapCapsule(
                    GetCrouchCheckPos,
                    crouchCheckSize,
                    crouchCheckDirection,
                    0f,
                    priv_WhatIsCeiling
                    ))
            {
                crouch = true;
            }

            Vector2 myGravity = (priv_Grounded) ?
                                Vector2.zero :
                                ((myVelocity.y < 0) ?
                                 new Vector2(0f, playerGravity * fallMultiplier) :
                                 ((jumpHold) ?
                                  new Vector2(0f, playerGravity * holdJumpFallMultiplier) :
                                  new Vector2(0f, playerGravity * lowJumpMultiplier)));

            // Set whether or not the character is crouching in the animator
            priv_Anim.SetBool("Crouch", crouch);
            if (crouch)
            {
                SetStateCrouch = PlayerStateCrouch.Crouch;
            }
            else
            {
                SetStateCrouch = PlayerStateCrouch.No;
            }

            // Reduce the speed if crouching by the crouchSpeed multiplier
            if (stateCrouch == PlayerStateCrouch.Crouch)
            {
                move = (crouch ? move * priv_CrouchSpeed : move);
            }

            //only control the player if grounded or airControl is turned on
            if (priv_Grounded || priv_AirControl)
            {
                // The Speed animator parameter is set to the absolute value of the horizontal input.
                priv_Anim.SetFloat("Speed", Mathf.Abs(move));

                // Move the character
                if (priv_Grounded)
                {
                    myVelocity = new Vector2(
                        move * myMaxSpeed * Mathf.Cos(avgNormAngle),
                        move * myMaxSpeed * Mathf.Sin(avgNormAngle));
                    if (myVelocity.x != 0)
                    {
                        stateGrounded = PlayerStateGrounded.Run;
                    }
                    else
                    {
                        stateGrounded = PlayerStateGrounded.Idle;
                    }
                }
                else
                {
                    myVelocity = new Vector2(move * myMaxSpeed, myVelocity.y);
                    if (myVelocity.y >= 0)
                    {
                        stateAerial = PlayerStateAerial.Jump;
                    }
                    else
                    {
                        stateAerial = PlayerStateAerial.Fall;
                    }
                }

                // If the input is moving the player right and the player is facing left...
                if (move > 0 && !priv_FacingRight)
                {
                    // ... flip the player.
                    Flip();
                }
                // Otherwise if the input is moving the player left and the player is facing right...
                else if (move < 0 && priv_FacingRight)
                {
                    // ... flip the player.
                    Flip();
                }
            }

            myVelocity += myGravity * Time.deltaTime;

            // If the player should jump...
            if (jump && !crouch)
            {
                // Tell the game that a jump input is waiting
                priv_jumpInput = true;
                if (airborneFrames > airborneGracePeriod)
                {
                    Debug.Log("air jump attempt");
                }
            }
            if (priv_jumpInput && (priv_Grounded || airborneFrames <= airborneGracePeriod))
            {
                // Add a vertical force to the player.
                myWallFollow = 0f;
                Debug.Log(airborneFrames);
                priv_jumpInput   = false;
                jumpBufferFrames = 0;
                priv_Grounded    = false;
                priv_Anim.SetBool("Ground", false);
                myVelocity = new Vector2(myVelocity.x, priv_JumpSpeed);
            }

            //Whim: How we set the player's move is dependant on the physics check
            priv_Rigidbody2D.velocity = myVelocity;
            transform.position       += new Vector3(0f, -myWallFollow, 0f);
        }