How to Make NPCs Look at the Player Only When Close (Unity Guide)

Neon Sketch Outline Effect in Unity URP
August 5, 2026
How to Create Footstep Particles in Unity (Dust, Grass, Snow, Water & More)
August 17, 2026
Neon Sketch Outline Effect in Unity URP
August 5, 2026
How to Create Footstep Particles in Unity (Dust, Grass, Snow, Water & More)
August 17, 2026

How to Make NPCs Look at the Player Only When Close (Unity Guide)

A great way to make NPCs feel alive is to make them look at the player — but ONLY when the player gets close enough.
This avoids creepy behavior (staring across the map!) and creates natural, realistic character interactions.

In this guide, you’ll learn how to make any Humanoid NPC look at the player with head tracking, using distance checks, angle limits, and smooth transitions.


1. Overview

We will use Unity’s OnAnimatorIK() method to rotate the NPC’s head
toward the player, but only when:

  • The player is within a certain distance
  • The player is within a certain angle (in front of the NPC)
  • The animation allows head tracking

2. Head Tracking Script (Distance-Based)

Attach this script to any NPC with a Humanoid Animator.

using UnityEngine;

public class NPCHeadTracking : MonoBehaviour
{
    public Transform player;            // Player transform
    public float lookDistance = 6f;     // Max distance to start looking
    public float lookAngle = 70f;       // Max angle from forward direction
    public float weight = 1f;           // IK strength
    public float smooth = 5f;           // Smooth interpolation speed

    private Animator animator;
    private float currentWeight;

    void Start()
    {
        animator = GetComponent<Animator>();
    }

    void OnAnimatorIK(int layerIndex)
    {
        if (player == null)
            return;

        float dist = Vector3.Distance(transform.position, player.position);

        // Too far → stop looking
        if (dist > lookDistance)
        {
            currentWeight = Mathf.Lerp(currentWeight, 0, Time.deltaTime * smooth);
            animator.SetLookAtWeight(currentWeight);
            return;
        }

        // Check angle
        Vector3 dir = (player.position - transform.position).normalized;
        float angle = Vector3.Angle(transform.forward, dir);

        // Player outside field of view
        if (angle > lookAngle)
        {
            currentWeight = Mathf.Lerp(currentWeight, 0, Time.deltaTime * smooth);
            animator.SetLookAtWeight(currentWeight);
            return;
        }

        // In range AND in front → track player
        currentWeight = Mathf.Lerp(currentWeight, weight, Time.deltaTime * smooth);

        animator.SetLookAtWeight(currentWeight, 0.2f, 1f, 1f, 0.5f);
        animator.SetLookAtPosition(player.position);
    }
}

3. What This Script Does

The NPC will:

  • Ignore the player if too far
  • Ignore the player if behind them
  • Smoothly turn their head only when appropriate
  • Blend naturally into and out of tracking

This creates believable, cinematic, human-like reactions.


4. Recommended Settings

  • lookDistance: 4–8 meters for natural behavior
  • lookAngle: 60–80 degrees
  • smooth: 4–8 for realistic head movement
  • weight: 0.7–1.0 depending on animation style

5. Giving NPCs a Small Delay (More Natural)

Humans don’t instantly react. Add a delay for realism:

public float reactionTime = 0.5f;
private float timer;

void Update()
{
    if (Vector3.Distance(transform.position, player.position) < lookDistance)
        timer += Time.deltaTime;
    else
        timer = 0;
}

Then inside OnAnimatorIK, only look when:

if (timer < reactionTime) return;

6. Optional: Only Look When Player Is Visible

Use a raycast for line-of-sight:

if (Physics.Raycast(
    transform.position + Vector3.up * 1.6f,
    (player.position - transform.position),
    out RaycastHit hit,
    lookDistance))
{
    if (hit.transform != player)
    {
        currentWeight = 0;
        return;
    }
}

This prevents looking through walls.


7. Optional: Stop Looking During Special Animations

If your NPC attacks, dies, or runs — you may want head tracking OFF:

bool canTrack = !animator.GetCurrentAnimatorStateInfo(0).IsTag("NoTrack");
if (!canTrack) currentWeight = 0;

Just add the tag NoTrack to animations that should disable head tracking.


8. Best Use Cases

  • Friendly NPCs reacting to the player
  • Shopkeepers
  • Guards and patrols noticing the player
  • Cinematic scenes
  • Dialogue triggers
  • Horror games (characters turn slowly toward the player 😨)

Conclusion

Making NPCs look at the player only when close is a simple but extremely powerful technique.
It adds realism, personality, and emotional presence to your characters — without changing your animations.

With distance checks, angle limits, and smooth IK blending, you can create NPC reactions that feel natural and alive in any Unity project.

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *


Skip to toolbar