Chase the Player
Part of the Physics and Gameplay section of Coddy's Unity journey. Lesson 50 of 63.
An enemy that chases the player needs a Rigidbody, so walls stop it and it can push and be pushed. The chase itself is a velocity aimed at the player, set once per physics step. This zombie walks toward whatever is in its player slot:
[SerializeField] private Transform player;
[SerializeField] private float speed = 2.5f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
Vector3 toPlayer = player.position - rb.position;
toPlayer.y = 0f;
Vector3 dir = toPlayer.normalized;
rb.linearVelocity = new Vector3(dir.x * speed, rb.linearVelocity.y, dir.z * speed);
}player.position - rb.position points from the zombie to the player. Its y is set to 0 so the zombie walks level when the player stands higher or lower, and normalized gives it length 1, so the zombie walks at speed whether the player is 2 units away or 20. The new velocity keeps the old y velocity, so gravity still pulls the zombie down off a step. Its Rigidbody has Freeze Rotation on, so it does not tip over as it walks.
A velocity you set makes a walker that starts and turns at once. A heavy enemy can be pushed with AddForce in the same FixedUpdate instead: it gathers speed, keeps its momentum and overshoots when the player steps aside. Avoid moving a chaser's Transform: that places it inside a wall as readily as anywhere else, and the physics engine can only push it back out afterwards.
Challenge
MediumA boulder rolls after the player. Write the Boulder script on the Boulder, a ball with a Rigidbody. The Inspector gives it player (the Player) and force (6). The Player script is ready: the arrows walk the player.
- Every physics step, push the boulder toward the player with
AddForce, with a force offorcewhether the player is near or far. - Push along the ground only, with no up or down part, even when the player stands on the ledge.
- Do not move its Transform or freeze its rotation: friction rolls it.
Press Play, run circles around the boulder, then Check.
Try it yourself
using UnityEngine;
public class Boulder : MonoBehaviour
{
[SerializeField] private Transform player;
[SerializeField] private float force = 6f;
private Rigidbody rb;
void Start()
{
rb = GetComponent<Rigidbody>();
}
void FixedUpdate()
{
// push the boulder toward the player, along the ground
}
}
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Physics and Gameplay
1Rigidbody
Gravity and MassPush with AddForceJump with an ImpulseFixedUpdate for PhysicsVelocityKinematic BodiesRecap: Launch Pad2Collisions
OnCollisionEnterWhat Did I Hit?Break on ImpactStanding on the GroundBounce and FrictionCollider ShapesRecap: Breakable Wall