using System.Collections.Generic; using UnityEngine; using UnityEngine.AI; [RequireComponent(typeof(NavMeshAgent))] public class SimpleNPCFollower : MonoBehaviour { [SerializeField] private Transform target; [SerializeField] private Animator animator; [SerializeField, Min(0.5f)] private float followDistance = 2.2f; [SerializeField, Min(0f)] private float giveSpaceDistance = 1.2f; [Header("Optional Formation")] [SerializeField] private bool useFormation; [SerializeField, Min(0.5f)] private float formationSpacing = 2f; private static readonly List followers = new List(); private NavMeshAgent agent; private float nextPathUpdate; private bool givingSpace; private void Awake() { agent = GetComponent(); if (animator == null) animator = GetComponentInChildren(); if (animator != null) animator.applyRootMotion = false; } private void OnEnable() { if (!followers.Contains(this)) followers.Add(this); } private void Update() { UpdateAnimation(); if (!agent.isOnNavMesh) return; if (target == null) { agent.ResetPath(); return; } if (Time.time < nextPathUpdate) return; nextPathUpdate = Time.time + 0.2f; Vector3 away = transform.position - target.position; away.y = 0f; float distance = away.magnitude; if (giveSpaceDistance > 0f && distance < giveSpaceDistance) givingSpace = true; if (giveSpaceDistance <= 0f || distance > giveSpaceDistance + 0.5f) givingSpace = false; // Basic lesson: head towards the player and stop at followDistance. Vector3 destination = target.position; agent.stoppingDistance = followDistance; // Optional bonus: replace the destination with our formation slot. if (useFormation) { destination = GetFormationPosition(); agent.stoppingDistance = 0.35f; } if (givingSpace) { if (away.sqrMagnitude < 0.01f) away = -target.forward; destination = target.position + away.normalized * (giveSpaceDistance + 0.8f); agent.stoppingDistance = 0.1f; } if (NavMesh.SamplePosition(destination, out NavMeshHit hit, 2f, agent.areaMask)) agent.SetDestination(hit.position); else agent.ResetPath(); } // Bonus: count only active formation followers with the same target. private Vector3 GetFormationPosition() { int index = 0; int count = 0; foreach (var follower in followers) { if (follower == null || !follower.useFormation || follower.target != target) continue; if (follower == this) index = count; count++; } int row = index / 2; bool alone = index == count - 1 && count % 2 == 1; float side = alone ? 0f : (index % 2 == 0 ? -0.5f : 0.5f) * formationSpacing; return target.position + target.right * side - target.forward * (followDistance + row * formationSpacing); } [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.SubsystemRegistration)] private static void ResetFollowers() => followers.Clear(); private void UpdateAnimation() { if (animator == null) return; // Use actual movement, so a stopped or blocked NPC returns to idle. Vector3 velocity = agent.velocity; velocity.y = 0f; float speed = velocity.magnitude; bool moving = speed > 0.08f; // These are the existing Synty Base Locomotion parameters. // Restore grounded state after disabling and re-enabling the NPC. animator.SetBool("IsGrounded", true); animator.SetFloat("IsStrafing", 0f); animator.SetFloat("ForwardStrafe", 1f); animator.SetFloat("MoveSpeed", speed, 0.1f, Time.deltaTime); animator.SetInteger("CurrentGait", !moving ? 0 : speed < 2f ? 1 : speed < 5f ? 2 : 3); animator.SetBool("MovementInputHeld", moving); animator.SetBool("IsStopped", !moving); animator.SetBool("IsWalking", moving && speed < 2f); } private void OnDisable() { followers.Remove(this); if (agent != null && agent.isOnNavMesh) agent.ResetPath(); } private void OnValidate() { followDistance = Mathf.Max(0.5f, followDistance); giveSpaceDistance = Mathf.Clamp(giveSpaceDistance, 0f, followDistance - 0.3f); } }