using UnityEngine; namespace Code.Gameplay { public class AICarController : MonoBehaviour { [SerializeField] private float speed = 15f; [SerializeField] private float stoppingDistance = 2f; [SerializeField] private float turnSpeed = 3f; private Vector3[] _waypoints; private int _currentWaypointIndex; public void SetRoute(Vector3[] waypoints) { _waypoints = waypoints; _currentWaypointIndex = 0; } private void Update() { if (_waypoints == null || _waypoints.Length == 0) return; var target = _waypoints[_currentWaypointIndex]; var direction = target - transform.position; if (direction.magnitude > 0.1f) { var targetRotation = Quaternion.LookRotation(direction, Vector3.up); transform.rotation = Quaternion.Slerp(transform.rotation, targetRotation, turnSpeed * Time.deltaTime); } transform.Translate(Vector3.forward * (speed * Time.deltaTime), Space.Self); if (direction.magnitude < stoppingDistance) { _currentWaypointIndex = (_currentWaypointIndex + 1) % _waypoints.Length; } } } }