-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNPCWalkAI.cs
More file actions
46 lines (39 loc) · 1.07 KB
/
NPCWalkAI.cs
File metadata and controls
46 lines (39 loc) · 1.07 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
38
39
40
41
42
43
44
45
46
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NPCWalkAI : MonoBehaviour
{
[Header("NPC")]
[SerializeField] private int walkTimeChange = 3;
[SerializeField] private GameObject npcDest;
[Header("X Position")]
[SerializeField] private int xMinPos;
[SerializeField] private int xMaxPos;
private int _xPos;
[Header("Z Position")]
[SerializeField] private int zMinPos;
[SerializeField] private int zMaxPos;
private int _zPos;
private void Start()
{
RangeInitialization()
StartCoroutine(RunRandomWalk());
}
private void Update()
{
transform.LookAt(npcDest.transform);
transform.position = Vector3.MoveTowards(transform.position, npcDest.transform.position, 0.01f * Time.timeScale);
}
private IEnumerator RunRandomWalk()
{
yield return new WaitForSeconds(walkTimeChange);
RangeInitialization()
StartCoroutine(RunRandomWalk());
}
private void RangeInitialization()
{
_xPos = Random.Range(xMinPos, xMaxPos);
_zPos = Random.Range(zMinPos, zMaxPos);
npcDest.transform.position = new Vector3(_xPos, 0, _zPos);
}
}