Answers for "unity ai agents enemy script"

C#
0

unity ai agents enemy script

using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;

public class Enim : MonoBehaviour
{
	//lr is watch distance and sr is stop distance
    public float lr = 20f, sr = 1;
    public Transform target;
    public NavMeshAgent agent;
    // Start is called before the first frame update
    void Start()
    {
        agent = GetComponent<NavMeshAgent>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float distance = Vector3.Distance(target.position, transform.position);

        if (distance <= lr)
        {
            agent.SetDestination(target.position);
        }
        if (distance <= sr)
        {
        	agent.enabled = false;
        }
    }

    void OnDrawGizmosSelected()
    {
        Gizmos.color = Color.red;
        Gizmos.DrawWireSphere(transform.position, lr);
        Gizmos.color = Color.blue;
        Gizmos.DrawWireSphere(transform.position, sr);
    }
}
Posted by: Guest on August-03-2021

C# Answers by Framework

Browse Popular Code Answers by Language