Menu
CoddyTech

Coins

Lesson 13 of 20 in Coddy's Unity Game: Sky Lanes course.

Coins give the player a reason to change lanes when nothing is in the way. Each piece gets a line of four in one lane, after its obstacles, at 4, 7, 10 and 13 along the piece and y 1.

A coin spins on its own, with its own script:

void Update()
{
    transform.Rotate(0f, 0f, spinSpeed * Time.deltaTime);
}

The coin prefab lies on its side, so its own z axis points up: turning around z spins it like a coin on a table.

Collecting is the Runner's job, in the same OnTriggerEnter: a coin adds one through game.AddCoin() and is destroyed.

challenge icon

Challenge

Easy
  1. In Coin add public float spinSpeed = 180f; and the Update above.
  2. In Road add coinPrefab, and at the end of Fill place 4 coins in a random lane at z 4f + i * 3f.
  3. In GameManager add public TMP_Text coinText;, public int coins; and public void AddCoin() (one more coin, then ShowScore()). ShowScore also sets coinText.text = "Coins: " + coins;.
  4. In Runner.OnTriggerEnter, else if (other.CompareTag("Coin")): add the coin and destroy it.

Try it yourself

using UnityEngine;

public class Runner : MonoBehaviour
{
    public float laneWidth = 2.5f;
    public float laneSpeed = 16f;
    public float jumpHeight = 1.6f;
    public float gravity = -40f;
    public float slideTime = 0.7f;

    public int lane;

    private CharacterController controller;
    private GameManager game;
    private float fallSpeed;
    private float slide;

    void Start()
    {
        controller = GetComponent<CharacterController>();
        game = FindFirstObjectByType<GameManager>();
    }

    void Update()
    {
        if (!game.over)
        {
            if (Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow)) lane = Mathf.Max(-1, lane - 1);
            if (Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow)) lane = Mathf.Min(1, lane + 1);
            bool jump = Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.Space);
            if (jump && controller.isGrounded)
            {
                fallSpeed = Mathf.Sqrt(jumpHeight * -2f * gravity);
                StopSlide();
            }
            if ((Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)) && controller.isGrounded)
            {
                slide = slideTime;
                Resize(0.5f);
            }
        }

        if (slide > 0f)
        {
            slide -= Time.deltaTime;
            if (slide <= 0f) StopSlide();
        }

        if (controller.isGrounded && fallSpeed < 0f) fallSpeed = -2f;
        fallSpeed += gravity * Time.deltaTime;

        float targetX = lane * laneWidth;
        float x = Mathf.MoveTowards(transform.position.x, targetX, laneSpeed * Time.deltaTime);
        Vector3 move = new Vector3(x - transform.position.x, fallSpeed * Time.deltaTime, 0f);
        controller.Move(move);
    }

    void StopSlide()
    {
        if (transform.localScale.y < 1f) Resize(1f);
        slide = 0f;
    }

    void Resize(float height)
    {
        controller.enabled = false;
        transform.position += Vector3.up * (height - transform.localScale.y);
        transform.localScale = new Vector3(1f, height, 1f);
        controller.enabled = true;
    }

    void OnTriggerEnter(Collider other)
    {
        if (game.over) return;
        if (other.CompareTag("Obstacle"))
        {
            game.GameOver();
        }
    }
}

All lessons in Unity Game: Sky Lanes

Practice on your own: Unity playground