Skip to content

Have NPC orient toward look target while navigating; fix nav agent startup race.#189

Open
zhuberty wants to merge 1 commit into
Facepunch:mainfrom
zhuberty:fix/npc-face-player-correctly
Open

Have NPC orient toward look target while navigating; fix nav agent startup race.#189
zhuberty wants to merge 1 commit into
Facepunch:mainfrom
zhuberty:fix/npc-face-player-correctly

Conversation

@zhuberty

Copy link
Copy Markdown

Summary

Makes NPCs orient their body toward their look target while navigating, so they face what they're chasing rather than the path direction. Also disables the NavMeshAgent's built-in rotation so it no longer fights manual body rotation, and fixes a startup race condition that caused the nav agent to immediately report failure before it had a chance to begin navigating.

Motivation & Context

Previously, when an NPC was chasing a player (or any look target), the body would only rotate to face the target once the angle exceeded MaxHeadAngle (45°). During navigation the NavMeshAgent was also overriding body rotation to match the path direction, causing the NPC to look sideways or away from the player while closing the gap. Additionally, animation blend parameters (move_x/move_y) were computed relative to the agent's path rotation rather than the body's actual facing direction.

A secondary issue caused the scientist NPC to immediately drop props it had just picked up: the nav agent re-issue logic and the failure check ran in the same frame, so GetStatus() saw !Agent.IsNavigating before the agent had a chance to start and returned Failed, ending the schedule prematurely.

Fixes:

Implementation Details

  • Code/Npcs/Tasks/MoveTo.cs — Body rotation now branches on whether a look target is set. When one is present (e.g. chasing a player), the body rotates to face the look target directly each frame regardless of angle. When no look target is set, the existing lateral-threshold behavior is preserved to avoid sideways walking.
  • code/Npcs/Layers/NavigationLayer.cs — Four changes:
    • Agent.UpdateRotation = false on start so the NavMeshAgent no longer overrides manual body rotation.
    • SetMove now passes Npc.WorldRotation instead of Agent.WorldRotation so animation blend parameters are computed relative to where the body is actually facing.
    • Added a re-issue block in OnUpdate that re-calls Agent.MoveTo if the navmesh finishes building while the agent isn't navigating (handles procedurally generated geometry).
    • Added a _timeSinceLastMoveIssued grace period (0.1s) in GetStatus() so the agent isn't immediately declared failed before it has had a chance to start navigating after a move is issued.

Screenshots / Videos (if applicable)

sbox.2026.04.17.14.10.37.mp4

Checklist

  • Code follows existing style and conventions
  • No unnecessary formatting or unrelated changes
  • Public APIs are documented (if applicable)
  • Unit tests added where applicable and all passing
  • I'm okay with this PR being rejected or requested to change 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant