Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
924 B
1
Indexable
Never
    public void Start()
    {
        agent = GetComponent<NavMeshAgent>();
        if(agent != null && KnifeController.currentTarget == null)
        {
            agent.speed = speed;
            agent.SetDestination(RandomNavMeshLocation());
        }
    }

    public void Update()
    {
        if (agent != null && agent.remainingDistance <= agent.stoppingDistance && KnifeController.currentTarget == null)
        {
            agent.SetDestination(RandomNavMeshLocation());
        }
    }

    public Vector3 RandomNavMeshLocation()
    {
        Vector3 finalPosition = Vector3.zero;
        Vector3 randomPosition =Random.insideUnitSphere * walkRadius;
        randomPosition += transform.position;

        if(NavMesh.SamplePosition(randomPosition, out NavMeshHit hit, walkRadius, 1))
        {
            finalPosition = hit.position;
        }
        return finalPosition;
    }