-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPoliceOfficer.cs
121 lines (98 loc) · 2.99 KB
/
PoliceOfficer.cs
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class PoliceOfficer : MonoBehaviour
{
/*
private Rigidbody policeRb;
public float speed = 100f;
private bool left;
// public float leftBound = 5f;
// public float rightBound = 15f;
// public bool forceRight;
// Start is called before the first frame update
void Start()
{
policeRb = GetComponent<Rigidbody>();
left = false;
// forceRight = true;
}
// Update is called once per frame
void Update()
{
if (policeRb.transform.position.z >= 9.9f)
{
left = true;
transform.Rotate(0, 180, 0);
}
if (left)
{
policeRb.transform.Translate(0f, 0f, -2 * Time.deltaTime);
}
if (policeRb.transform.position.z <= 3.9f)
{
left = false;
}
if (!left)
{
policeRb.transform.Translate(0f, 0f, 2 * Time.deltaTime);
}
/* if (transform.position.z > 14)
{
policeRb.AddForce(Vector3.forward * -speed);
}
else if (transform.position.z < 6)
{
policeRb.AddForce(Vector3.forward * speed);
}
}
*/
// Patrol.cs
public Animator policeAnim;
public Transform[] points;
private int destPoint = 0;
private NavMeshAgent agent;
private GameManager gameManager;
void Start()
{
policeAnim = GetComponent<Animator>();
agent = GetComponent<NavMeshAgent>();
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
// Disabling auto-braking allows for continuous movement
// between points (ie, the agent doesn't slow down as it
// approaches a destination point).
agent.autoBraking = true;
if (gameManager.isGameActive == true)
{
GotoNextPoint();
}
}
void GotoNextPoint()
{
// Returns if no points have been set up
if (points.Length == 0)
return;
// Set the agent to go to the currently selected destination.
agent.destination = points[destPoint].position;
// Choose the next point in the array as the destination,
// cycling to the start if necessary.
destPoint = (destPoint + 1) % points.Length;
}
void Update()
{
if (gameManager.isGameActive == true)
{
OfficerMovement();
}
}
public void OfficerMovement()
{
policeAnim.SetFloat("Speed_f", .3f);
policeAnim.SetBool("Static_b", true);
// Choose the next destination point when the agent gets
// close to the current one.
if (!agent.pathPending && agent.remainingDistance < 0.5f)
GotoNextPoint();
}
}