UnityScripterAI
Discord ↗
Link Copied to Clipboard
×
"AI System with detection, smart navmesh and intuitive ai."
using UnityEngine; using UnityEngine.AI; public class AISystem : MonoBehaviour { public Transform target; public float detectionRange = 10f; public float updatePathInterval = 0.5f; private NavMeshAgent agent; private float elapsedTime; // Start is called before the first frame update void Start() { agent = GetComponent<NavMeshAgent>(); elapsedTime = 0f; } // Update is called once per frame void Update() { elapsedTime += Time.deltaTime; if (IsTargetInRange() && elapsedTime > updatePathInterval) { UpdatePathToTarget(); elapsedTime = 0f; } } bool IsTargetInRange() { float distanceToTarget = Vector3.Distance(transform.position, target.position); return distanceToTarget <= detectionRange; } void UpdatePathToTarget() { agent.SetDestination(target.position); } }