Menu
CoddyTech

Hunters

Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 60 of 71.

challenge icon

Challenge

Medium

Fill in Hunter so the hunters chase the player. Player is done; leave it as it is.

  1. Add [SerializeField] private float speed = 3f; and private Transform player;. In Start(), set player = GameObject.FindWithTag("Player").transform;.
  2. In FixedUpdate(), take player.position - rb.position, set its y to 0 and normalize it.
  3. Set rb.linearVelocity to its x and z times speed, keeping rb.linearVelocity.y.

Press Play and keep away. Then Check.

Try it yourself

using UnityEngine;

public class Hunter : MonoBehaviour
{
    private Rigidbody rb;

    void Start()
    {
        rb = GetComponent<Rigidbody>();
    }

    // Chase the player: find it by its tag, then run at it every physics step
}

All lessons in Physics and Gameplay

Practice on your own: Unity playground