예제 #1
0
    private void FixedUpdate()
    {
        //----------Side and ground collision----------

        // Tests if the player is in the air or not
        Ray groundDetectionRay = new Ray(transform.position + new Vector3(0, 0.01f, 0), new Vector3(0, -1, 0));
        //Debug.DrawLine(groundDetectionRay.origin, groundDetectionRay.origin + (Vector3.up * -maxRejumpDistance));
        RaycastHit groundDetectionRayHitInfo;

        if (Physics.Raycast(groundDetectionRay, out groundDetectionRayHitInfo, maxRejumpDistance) && groundDetectionRayHitInfo.collider.name.Contains("Floor"))
        {
            //Debug.Log("Found the ground!");
            isGrounded = true;
        }
        else
        {
            isGrounded = false;
        }

        // Sends a ray out to the left of the player to make sure they can't stick to a wall
        Ray leftWallRay = new Ray(transform.position + new Vector3(-0.49f, 0, 0), new Vector3(-1, 0, 0));
        //Debug.DrawLine(leftWallRay.origin, leftWallRay.origin + (-Vector3.right * sideHitDetectionDistance));
        RaycastHit leftWallRayHitInfo;

        if (Physics.Raycast(leftWallRay, out leftWallRayHitInfo, sideHitDetectionDistance) && leftWallRayHitInfo.collider.name.Contains("Wall"))
        {
            //Debug.Log("Left wall hit!");
            leftWallHit = true;
        }
        else
        {
            leftWallHit = false;
        }

        // Sends a ray out to the right of the player to make sure they can't stick to a wall
        Ray rightWallRay = new Ray(transform.position + new Vector3(0.49f, 0, 0), new Vector3(1, 0, 0));
        //Debug.DrawLine(rightWallRay.origin, rightWallRay.origin + (Vector3.right * sideHitDetectionDistance));
        RaycastHit rightWallRayHitInfo;

        if (Physics.Raycast(rightWallRay, out rightWallRayHitInfo, sideHitDetectionDistance) && rightWallRayHitInfo.collider.name.Contains("Wall"))
        {
            //Debug.Log("Right wall hit!");
            rightWallHit = true;
        }
        else
        {
            rightWallHit = false;
        }

        //----------Timers----------

        // Prevents the player from spamming the jump button
        if (jumpTimer >= 0)
        {
            jumpTimer -= Time.deltaTime;
        }
        else
        {
            jumpTimer = 0;
        }

        // Prevents the player from spamming the slide button
        if (!standing)
        {
            if (slideTimer == 0)
            {
                standing = true;
            }
            else
            {
                slideTimer -= Time.deltaTime;
                if (slideTimer < 0)
                {
                    slideTimer = 0;
                }
            }
        }

        //----------Player movement----------

        // Moves the player left
        if ((Input.GetKey(moveLeft) || Input.GetAxis("Horizontal") < 0) && !leftWallHit && !dController.HasPlayerCollided())
        {
            if (isGrounded && rb.velocity.magnitude < groundMaxMoveSpeed)
            {
                rb.AddForce(new Vector3(-groundMoveSpeed, 0, 0));
            }
            else if (!isGrounded)
            {
                float LRValue = Vector3.Dot(transform.right, rb.velocity);
                if (rb.velocity.magnitude < airMaxMoveSpeed)
                {
                    rb.AddForce(new Vector3(-airMoveSpeed, 0, 0));
                }
                else
                {
                    if (LRValue > 0f)   // Player currently moving right
                    {
                        rb.AddForce(new Vector3(-airMoveSpeed, 0, 0));
                    }
                }
            }
        }

        // Moves the player right
        if ((Input.GetKey(moveRight) || Input.GetAxis("Horizontal") > 0) && !rightWallHit && !dController.HasPlayerCollided())
        {
            if (isGrounded && rb.velocity.magnitude < groundMaxMoveSpeed)
            {
                rb.AddForce(new Vector3(groundMoveSpeed, 0, 0));
            }
            else if (!isGrounded)
            {
                float LRValue = Vector3.Dot(transform.right, rb.velocity);
                if (rb.velocity.magnitude < airMaxMoveSpeed)
                {
                    rb.AddForce(new Vector3(airMoveSpeed, 0, 0));
                }
                else
                {
                    if (LRValue < 0f)   // Player currently moving left
                    {
                        rb.AddForce(new Vector3(airMoveSpeed, 0, 0));
                    }
                }
            }
        }


        // Makes the player slide
        if ((Input.GetKey(slide) || Input.GetButton("Cancel")) && slideTimer == 0 && isGrounded && standing && !dController.HasPlayerCollided())
        {
            standing   = false;
            slideTimer = slideDelay;
            // Play sliding animation
            animator.Play("SlideAnimation");
        }

        // Makes the player jump
        if ((Input.GetKey(jump) || Input.GetButton("Submit")) && jumpTimer == 0 && isGrounded && standing && !dController.HasPlayerCollided())
        {
            rb.AddForce(new Vector3(0, 1, 0) * jumpForce, ForceMode.Impulse);
            jumpTimer = jumpDelay;
        }

        // Stops the running animation if the player has died
        if (dController.hasDied)
        {
            animator.Play("PlayerStill");
        }

        //Debug.Log(rb.velocity.magnitude);

        //----------Front Collision----------

        //Ray drawn on the bottom left of the player to detect collsion between frames
        Ray rayHitDetectBottomLeft = new Ray(transform.position + new Vector3(-0.5f, 0.5f, 0), new Vector3(0, 0, 1));
        //Debug.DrawLine(rayHitDetectBottomLeft.origin, rayHitDetectBottomLeft.origin + (Vector3.forward * frontHitDetectionDistance));
        RaycastHit rayHitDetectBottomLeftHitInfo;

        if (Physics.Raycast(rayHitDetectBottomLeft, out rayHitDetectBottomLeftHitInfo, frontHitDetectionDistance))
        {
            if (rayHitDetectBottomLeftHitInfo.collider.name.Contains("Obstacle"))
            {
                //Debug.Log("Left Ray Hit");
                dController.ObstacleCollide();
            }
        }

        //Ray drawn on the bottom right of the player to detect collsion between frames
        Ray rayHitDetectBottomRight = new Ray(transform.position + new Vector3(0.5f, 0.5f, 0), new Vector3(0, 0, 1));
        //Debug.DrawLine(rayHitDetectBottomRight.origin, rayHitDetectBottomRight.origin + (Vector3.forward * frontHitDetectionDistance));
        RaycastHit rayHitDetectBottomRightHitInfo;

        if (Physics.Raycast(rayHitDetectBottomRight, out rayHitDetectBottomRightHitInfo, frontHitDetectionDistance))
        {
            if (rayHitDetectBottomRightHitInfo.collider.name.Contains("Obstacle"))
            {
                //Debug.Log("Right Ray Hit");
                dController.ObstacleCollide();
            }
        }

        if (standing)
        {
            //Ray drawn on the top left of the player to detect collsion between frames
            Ray rayHitDetectTopLeft = new Ray(transform.position + new Vector3(-0.5f, 2.25f, 0), new Vector3(0, 0, 1));
            //Debug.DrawLine(rayHitDetectTopLeft.origin, rayHitDetectTopLeft.origin + (Vector3.forward * frontHitDetectionDistance));
            RaycastHit rayHitDetectTopLeftHitInfo;
            if (Physics.Raycast(rayHitDetectTopLeft, out rayHitDetectTopLeftHitInfo, frontHitDetectionDistance))
            {
                if (rayHitDetectTopLeftHitInfo.collider.name.Contains("Obstacle"))
                {
                    //Debug.Log("Left Ray Hit");
                    dController.ObstacleCollide();
                }
            }

            //Ray drawn on the top right of the player to detect collsion between frames
            Ray rayHitDetectTopRight = new Ray(transform.position + new Vector3(0.5f, 2.25f, 0), new Vector3(0, 0, 1));
            //Debug.DrawLine(rayHitDetectTopRight.origin, rayHitDetectTopRight.origin + (Vector3.forward * frontHitDetectionDistance));
            RaycastHit rayHitDetectTopRightHitInfo;
            if (Physics.Raycast(rayHitDetectTopRight, out rayHitDetectTopRightHitInfo, frontHitDetectionDistance))
            {
                if (rayHitDetectTopRightHitInfo.collider.name.Contains("Obstacle"))
                {
                    //Debug.Log("Right Ray Hit");
                    dController.ObstacleCollide();
                }
            }
        }

        // Stops player physics if they have hit an obstacle
        if (dController.HasPlayerCollided())
        {
            rb.isKinematic = true;
        }
    }
예제 #2
0
    private void FixedUpdate()
    {
        // Controls difficulty based on score (distance travelled)
        if (sController.Score > difficulty2Score && diffiulty == 0)
        {
            diffiulty = 1;
        }
        else if (sController.Score > difficulty3Score && diffiulty == 1)
        {
            diffiulty = 2;
        }
        else if (sController.Score > difficulty4Score && diffiulty == 2)
        {
            diffiulty = 3;
        }

        // Resets the newPlatforms and deletedPlatforms list
        newPlatforms.Clear();
        deletedPlatforms.Clear();

        // For each platform in the platforms list we check if the position is out of player sight and spawns a new platform at the end of the line,
        // then deletes the current platform and adds it to the deletedPlatforms list
        foreach (GameObject platform in platforms)
        {
            if (platform.transform.position.z < -100)
            {
                // Randomizes the platforms then accounts for difficulty
                randomNo = Random.Range(0, 5);
                //Debug.Log(randomNo);
                if (randomNo > 4)
                {
                    // Creates an empty platform
                    GameObject newPlatform = Instantiate(platform_Empty, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                    newPlatforms.Add(newPlatform);
                }
                else
                {
                    // Has sets of platforms based on the difficulty which scales based on score
                    switch (diffiulty)
                    {
                    // Difficulty 1
                    case 0:
                        if (randomNo == 0)
                        {
                            GameObject newPlatform = Instantiate(platform_D0_1, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 1)
                        {
                            GameObject newPlatform = Instantiate(platform_D0_2, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 2)
                        {
                            GameObject newPlatform = Instantiate(platform_D0_3, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 3)
                        {
                            GameObject newPlatform = Instantiate(platform_D0_4, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 4)
                        {
                            GameObject newPlatform = Instantiate(platform_D0_5, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        break;

                    // Difficulty 2
                    case 1:
                        if (randomNo == 0)
                        {
                            GameObject newPlatform = Instantiate(platform_D1_1, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 1)
                        {
                            GameObject newPlatform = Instantiate(platform_D1_2, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 2)
                        {
                            GameObject newPlatform = Instantiate(platform_D1_3, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 3)
                        {
                            GameObject newPlatform = Instantiate(platform_D1_4, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 4)
                        {
                            GameObject newPlatform = Instantiate(platform_D1_5, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        break;

                    // Difficulty 3
                    case 2:
                        if (randomNo == 0)
                        {
                            GameObject newPlatform = Instantiate(platform_D2_1, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 1)
                        {
                            GameObject newPlatform = Instantiate(platform_D2_2, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 2)
                        {
                            GameObject newPlatform = Instantiate(platform_D2_3, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 3)
                        {
                            GameObject newPlatform = Instantiate(platform_D2_4, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 4)
                        {
                            GameObject newPlatform = Instantiate(platform_D2_5, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        break;

                    // Difficulty 4
                    case 3:
                        if (randomNo == 0)
                        {
                            GameObject newPlatform = Instantiate(platform_D3_1, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 1)
                        {
                            GameObject newPlatform = Instantiate(platform_D3_2, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 2)
                        {
                            GameObject newPlatform = Instantiate(platform_D3_3, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 3)
                        {
                            GameObject newPlatform = Instantiate(platform_D3_4, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        else if (randomNo == 4)
                        {
                            GameObject newPlatform = Instantiate(platform_D3_5, new Vector3(0, -0.5f, platform.transform.position.z + 1800.0f), Quaternion.identity);
                            newPlatforms.Add(newPlatform);
                        }
                        break;
                    }
                }
                // Removes the platform behind the player
                Destroy(platform);
                deletedPlatforms.Add(platform);
            }
        }

        // Clears the deleted platforms list
        foreach (GameObject platform in deletedPlatforms)
        {
            platforms.Remove(platform);
        }

        // Adds all new platforms into the main platforms list
        platforms.AddRange(newPlatforms);

        // Moves every platform to simulate player movement
        foreach (GameObject platform in platforms)
        {
            if (dController.HasPlayerCollided() == false)
            {
                Vector3 newPosition = platform.transform.position + new Vector3(0, 0, -platformMoveSpeed * Time.deltaTime);
                platform.transform.position = newPosition;
            }
        }
    }