Exemple #1
0
    // Update is called once per frame
    void FixedUpdate()
    {
        // checks if the player is contacting the ground
        isOnGround = Physics2D.OverlapCircle(groundChecker.position, groundCheckerRadius, groundLayer);

        // checks if player's head has hit the ground
        if (Physics2D.OverlapCircle(headChecker.position, headCheckerRadius, groundLayer))
        {
            LocalDestroy();
        }

        // prevents character from move faster than the max speed;
        if (mRB.velocity.magnitude >= maxSpeed)
        {
            mRB.velocity = mRB.velocity.normalized * maxSpeed;
        }

        // player can only rotate when in the air
        if (!isOnGround && !isAboveRail)
        {
            mRB.MoveRotation(mRB.rotation - Input.GetAxis("Horizontal") * rotationSpeed * Time.fixedDeltaTime);
            // mRB.AddTorque(Input.GetAxis("Horizontal") * -rotationSpeed);  different rotation method feels different
        }

        /*
         * if (isAboveRail)
         * {
         *  gb.UpdateScore(2);
         *  if (Input.GetAxis("Vertical") < 0)
         *      mRB.AddForce(transform.right * grindSpeed);
         * }
         */

        // apply speed boost for x amount of seconds
        if (boost && trickComplete)
        {
            speedBoostTime -= Time.deltaTime;
            if (speedBoostTime > 0f)
            {
                mRB.AddForce(Vector3.Normalize(transform.right) * speedMultiplier);
                gb.updateTimer(2f, speedBoostTime);
            }
            else
            {
                boost           = false;
                trickComplete   = false;
                speedMultiplier = 1f;
                speedBoostTime  = 2f; // reset timer
            }
        }

        // update text
        if (speedMultiplier == 1f)
        {
            gb.UpdateSpeedMulText("");
        }
        else
        {
            gb.UpdateSpeedMulText("Boost X " + speedMultiplier);
        }

        if (jumped)
        {
            CheckForTricks();
        }
    }