Exemplo n.º 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))
        {
            gb.DestroyMe();
        }

        // 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)
        {
            speedBoostTime -= Time.deltaTime;
            if (speedBoostTime > 0)
            {
                ApplySpeedBoost();
            }
            else
            {
                boost           = false;
                speedMultiplier = 1;
                speedBoostTime  = 2f; // reset timer
            }
        }

        // update text
        gb.UpdateSpeedMulText("Speed multiplier: " + speedMultiplier.ToString());

        if (jumped)
        {
            CheckForTricks();
        }
    }
Exemplo n.º 2
0
    void OnCollisionEnter2D(Collision2D other)
    {
        #region ground trigger
        if (other.gameObject.CompareTag("GroundCollider") && jumped)
        {
            float angle = Vector2.Angle(this.transform.right, rayCastRight.point - rayCastLeft.point);

            if (angle <= 15f)
            {
                int reward = 20;

                jumped = false;

                if (trickComplete)
                {
                    BoostPlayer(2f);
                }

                gb.UpdateLandingText("Landing: Perfect! +" + reward);
                gb.UpdateScore(reward);
            }
            else if (angle <= 30f)
            {
                int reward = 15;

                if (trickComplete)
                {
                    BoostPlayer(1.8f);
                }

                jumped = false;

                gb.UpdateLandingText("Landing: Great! +" + reward);
                gb.UpdateScore(reward);
            }
            else if (angle <= 45f)
            {
                int reward = 10;

                if (trickComplete)
                {
                    BoostPlayer(1.6f);
                }

                jumped = false;

                gb.UpdateLandingText("Landing: Good! +" + reward);
                gb.UpdateScore(reward);
            }
            else if (angle <= 60f)
            {
                int reward = 5;

                if (trickComplete)
                {
                    BoostPlayer(1.2f);
                }

                jumped = false;

                gb.UpdateLandingText("Landing: Alright +" + reward);
                gb.UpdateScore(reward);
            }
            else if (angle <= 90f)
            {
                int reward = 2;

                if (trickComplete)
                {
                    BoostPlayer(0.5f);
                }

                jumped = false;

                gb.UpdateLandingText("Landing: Poor +" + reward);
                gb.UpdateScore(reward);
            }
            else
            {
                int reward = 0;
                gb.UpdateLandingText("Landing: CRASH! +" + reward);
                LocalDestroy();
            }

            Vector3    targetVector  = (rayCastRight.point - rayCastLeft.point).normalized;
            Quaternion toRotation    = Quaternion.LookRotation(targetVector, Vector3.up);
            float      maxAngleDelta = 15f;

            // rotates the player to be parallel to the ground
            transform.rotation = mRB.velocity.x > 0 ? Quaternion.RotateTowards(transform.rotation, toRotation, maxAngleDelta)
                : Quaternion.RotateTowards(transform.rotation, Quaternion.LookRotation(-targetVector, Vector3.up), maxAngleDelta);

            mRB.AddForce(Vector3.Normalize(transform.right) * AddedSpeed);
        }

        #endregion

        #region rail trigger

        /*
         * if (other.gameObject.CompareTag("GrindObject"))
         * {
         *  isAboveRail = true;
         *  if (Input.GetAxis("Vertical") >= 0)
         *      LocalDestroy();
         * }
         * else
         *  isAboveRail = false;
         */
        #endregion
    }