EndlessRunner

: Unity, : C#, : JetBrains Rider, : 2 weeks, : School, : 4 people

In this project I worked with 3 other students. We were placed in random groups of students, so we can work with other students we have not worked with. The time we had for this project was 2 weeks. The theme of the project was to make an endless runner game. We decided on base of references from other games how the game was going to look. We decided to get kind of the same art style from Cyberpunk 2077. After we have decided the art style we went to go to smaller groups and discuss what we needed. Such as: are we going to move the player or are we going to move the map? How are we going to save the high-scores? How are we going to get the score of the player? After that we were going to assign tasks to each other. And we did give each other 2x a day updates because of the scrum methode.

What did I learn in this project?

I have learned how I can work with the new player controls. I also learned how I can make a shop.

What did I do in this project?

I got the following tasks: Building ScoreManager, Building High-score system, Building Playercontrols, Building ShopManager, Game loop and UI components such as: Numbers, Lap time, Score and High-score.

Code snippets

Player Movement

[Tooltip("How fast the player moves")]
    [SerializeField] private float speed = 5;
    [Tooltip("How much the player can move side ways")]
    [SerializeField] private float sideMovement = 5;
    [Tooltip("How far the player can move to the sides")]
    [SerializeField] private float clampValue = 5;
    [Tooltip("How smooth the side movement is")]
    [SerializeField] private float sideSmooth = 3;
    [Tooltip("A multiplier for the side movement")]
    [SerializeField] private AnimationCurve speedMultiplier;

    private PlayerControls playerControls;
    private Rigidbody rb;
    private float newXPosition;
    private float xMovement;
    private Lanes currentLane;

    private void Awake()
    {
        playerControls = new PlayerControls();
        rb = GetComponent();
        currentLane = Lanes.Middle;
        newXPosition = transform.position.x;
    }

    private void Update()
    {
        if (GameManager.instance.GetGameState() != GameState.Game)
            return;

        Move();
    }

    private void Move()
    {
        if (playerControls.Player.Left.triggered)
        {
            if (currentLane == Lanes.Middle)
            {
                newXPosition = -sideMovement;
                currentLane = Lanes.Left;
            }
            else if (currentLane == Lanes.Right)
            {
                newXPosition = 0;
                currentLane = Lanes.Middle;
            }
        }
        if (playerControls.Player.Right.triggered)
        {
            if (currentLane == Lanes.Middle)
            {
                newXPosition = sideMovement;
                currentLane = Lanes.Right;
            }
            else if (currentLane == Lanes.Left)
            {
                newXPosition = 0;
                currentLane = Lanes.Middle;
            }
        }

        xMovement = Mathf.Lerp(xMovement, newXPosition, Time.deltaTime * sideSmooth * speedMultiplier.Evaluate(Time.deltaTime));
        transform.position = new Vector3(xMovement, transform.position.y, transform.position.z);
        transform.position = new Vector3(Mathf.Clamp(transform.position.x, -clampValue, clampValue), transform.position.y, transform.position.z);
    }

Distance + Score + Highscore changer

private void Update()
    {
        if (p_Time < 3 && GameManager.instance.GetGameState() == GameState.Lost)
            AchievementManager.instance.EarnAchievement("WATCH THE ROAD");

        if (GameManager.instance.GetGameState() != GameState.Game)
            return;

        // Will add time to the 2 timers
        p_Timer += Time.deltaTime;
        p_Time += Time.deltaTime;

        // Formula used: Distance = speed x time so we will get the distance
        p_Distance = p_speed * p_Time;

        // Change the time when it is above the given change
        if (p_Timer > p_PointTime)
            TimeChanger();

        // Change the highscore when it is above it
        if (p_DistanceInt > p_HighScore)
            HighScoreChanger();

        //change the distance when its above 0
        if (p_Distance > 0.05)
            DistanceChanger();

        if (p_Distance > 100)
            AchievementManager.instance.EarnAchievement("Drive");
        if (p_Distance > 1000)
            AchievementManager.instance.EarnAchievement("To the horizon");
    }

    #region Time Changer
    private void TimeChanger()
    {
        // adds a point to the score
        p_TimerScore++;

        PlayerPrefs.SetInt("Time", p_TimerScore);
        PlayerPrefs.Save();

        // change the score
        ChangeTimeText();

        // resets the timer
        p_Timer = 0f;
    }

    // change the Time text when it is summoned
    private void ChangeTimeText()
    {
        p_ScoreText.text = p_TimerScore.ToString();
    }
    #endregion

    #region Distance and HighScore Changer

    /// Will change the distance given in the update to an int
    /// So it will be used in the score shown on the screen
    private void DistanceChanger()
    {
        p_DistanceInt = Mathf.RoundToInt(p_Distance);

        PlayerPrefs.SetInt("CurrentDistance", p_DistanceInt);
        PlayerPrefs.Save();

        p_DistanceText.text = p_DistanceInt + "m";
    }

    /// Will change the highscore if the distance is higher.
    /// Will also update when the highscore is beaten
    /// Will also save the highscore in playerprefs.
    private void HighScoreChanger()
    {
        p_HighScore = p_DistanceInt;
        p_HighScoreText.text = p_DistanceInt + "m";

        PlayerPrefs.SetInt("HighScore", p_HighScore);
        PlayerPrefs.Save();
    }
    #endregion

All the code from this project:
Github Code