-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBot.cs
More file actions
37 lines (35 loc) · 1.13 KB
/
Bot.cs
File metadata and controls
37 lines (35 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI; //for NavMeshAgent
[RequireComponent(typeof(NavMeshAgent))]
public class Bot : MonoBehaviour
{
public float MinTime = 2;
public float MaxTime = 5;
public GameObject Ground = null;
private NavMeshAgent nma = null;
private Bounds bounds;
private void Start()
{
nma = this.GetComponent<NavMeshAgent>();
bounds = Ground.GetComponent<Renderer>().bounds;
}
private void Update()
{
if (nma.hasPath == false || nma.remainingDistance < 1.0f)
{
float wait = Random.Range(MinTime, MaxTime);
this.GetComponent<Renderer>().material.color = Color.red;
Invoke("PickRandomDestination", wait);
}
}
private void PickRandomDestination()
{
float rx = Random.Range(bounds.min.x, bounds.max.x);
float rz = Random.Range(bounds.min.z, bounds.max.z);
Vector3 rpos = new Vector3(rx, this.transform.position.y, rz);
nma.SetDestination(rpos);
this.GetComponent<Renderer>().material.color = Color.green;
}
}