Menu
CoddyTech

Recap: Guard Dog

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

This recap puts the chapter together: a chase set as a velocity, a range measured across the ground, damage through a Health script and a knockback impulse.

challenge icon

Challenge

Medium

Write the guard dog: it chases strangers near its kennel.

  1. In FixedUpdate(), while the player is under guardRange from kennel.position across the ground, run at the player at speed, keeping y.
  2. Otherwise run back to the kennel; within 0.3 of it, stand still.
  3. On bumping the Player, call its Health's TakeDamage(1) and give it an Impulse of bite away from the dog, flat.

Press Play near the kennel. Then Check.

Try it yourself

using UnityEngine;

public class Dog : MonoBehaviour
{
    [SerializeField] private Transform player;
    [SerializeField] private Transform kennel;
    [SerializeField] private float speed = 3.5f;
    [SerializeField] private float guardRange = 6f;
    [SerializeField] private float bite = 5f;
    private Rigidbody rb;

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

    void FixedUpdate()
    {
        // guard the kennel: chase the player near it, or go home
    }
}
quiz iconTest yourself

This lesson includes a short quiz. Start the lesson to answer it and track your progress.

All lessons in Physics and Gameplay

Practice on your own: Unity playground