Menu
CoddyTech

Magnet

Lesson 14 of 21 in Coddy's Unity Game: Sky Lanes course.

Power-ups make a run feel different from the last. The first is a magnet: for 8 seconds every coin near the runner flies to it.

The Runner keeps the time left in magnet and counts it down. Each coin looks at the runner, and while the magnet is on and the runner is near, moves towards it:

if (runner.magnet > 0f && Vector3.Distance(transform.position, runner.transform.position) < pullRange)
{
    transform.position = Vector3.MoveTowards(transform.position, runner.transform.position, pullSpeed * Time.deltaTime);
}

The coin flies into the runner and its trigger does the rest. Now and then a piece of road gets a pink magnet pickup, 6 units before its obstacles.

challenge icon

Challenge

Easy

A magnet pulls nearby coins to the runner.

  1. Runner: add magnetTime = 8f and magnet; count magnet down while above 0.
  2. A Magnet tag sets magnet = magnetTime and destroys the pickup.
  3. Coin: add pullRange = 7f and pullSpeed = 30f, and pull as above.
  4. Road.Fill: with float roll = Random.value;, place magnetPrefab in a random lane at y 1, z -6 when roll < 0.06f.

Press Play and grab the pink magnet: coins fly to you. Then Check.

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();
        }
        else if (other.CompareTag("Coin"))
        {
            game.AddCoin();
            Destroy(other.gameObject);
        }
    }
}

All lessons in Unity Game: Sky Lanes

Practice on your own: Unity playground