Exemplo n.º 1
0
    // Movement of the player

    void Move(Vector2 inputDir, bool running)
    {
        if (inputDir != Vector2.zero)
        {
            // When the player is not pushing we can create a moving direction
            if (!playerIsPushing)
            {
                float targetRotation = Mathf.Atan2(inputDir.x, inputDir.y) * Mathf.Rad2Deg + cameraT.eulerAngles.y;
                transform.eulerAngles = Vector3.up * Mathf.SmoothDampAngle(transform.eulerAngles.y, targetRotation, ref turnSmoothVelocity, GetModifiedSmoothTime(turnSmoothTime));
            }
        }

        float targetSpeed = ((running) ? runSpeed : movementSpeed) * inputDir.magnitude;

        currentSpeed = Mathf.SmoothDamp(currentSpeed, targetSpeed, ref speedSmoothVelocity, GetModifiedSmoothTime(speedSmoothTime));

        velocityY += Time.deltaTime * gravity;

        Vector3 velocity = transform.forward * currentSpeed + Vector3.up * velocityY;

        controller.Move(velocity * Time.deltaTime);

        currentSpeed = new Vector2(controller.velocity.x, controller.velocity.z).magnitude;

        float animationSpeedPercent = ((running) ? currentSpeed / runSpeed : currentSpeed / movementSpeed * .5f);

        // Checks if player is not grounded and is falling
        if (GroundCheck())
        {
            velocityY = 0;

            // When the player is grounded the slopeRays are active
            slopeRay = false;

            // Sets the airtimer to 0 when the player is grounded
            airTimer = 0;

            if (heightDiff > minHardLanding && heightDiff < maxHardLanding)
            {
                PlayerLandedYPos();

                anim.SetBool("hardLanding", true);
                anim.SetBool("onAir", false);
                anim.SetBool("onAirIdle", false);
            }
            else if (heightDiff > playerDeathHeight)
            {
                gravity = -9.81f;
                playerDeath.CallPlayerDeath();
            }
            else
            {
                anim.SetBool("hardLanding", false);
                anim.SetBool("onAir", false);
                anim.SetBool("onAirIdle", false);

                isJumping = false;
                PlayerLandedYPos();
            }
        }
        else
        {
            // When the player is jumping or falling the slopeRays are inactive
            slopeRay = true;

            // Starts a timer when the player is in air
            airTimer += Time.deltaTime;

            lastVelocityY = controller.velocity.y;

            // Check animationSpeedPercent to determine which air state
            if (animationSpeedPercent < 0.51f)
            {
                // When the player is in air we wait for 0.15 sec before the airstate is set in the animator. We do this to make sure the player
                // isn't falling for very short periods when the ground state isn't true for very short times.
                if (airTimer > 0.15f)
                {
                    anim.SetBool("onAirIdle", true);
                }
            }
            else
            {
                // When the player is in air we wait for 0.15 sec before the airstate is set in the animator. We do this to make sure the player
                // isn't falling for very short periods when the ground state isn't true for very short times.
                if (airTimer > 0.15f)
                {
                    anim.SetBool("onAir", true);
                }
            }
            anim.SetBool("hardLanding", false);

            PlayerLandedYPos();
        }
    }