-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathChaser.cs
52 lines (42 loc) · 1.18 KB
/
Chaser.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
using UnityEngine;
using System.Collections;
/// <summary>
/// Setup the chase behavior of a general enemy game object.
/// </summary>
public class Chaser : MonoBehaviour {
public float speed = 20.0f;
public float minDist = 1f;
public Transform target;
/// <summary>
/// Use this for initialization.
/// </summary>
void Start () {
// If no target specified, assume the player.
if (target == null) {
if (GameObject.FindWithTag ("Player")!=null)
{
target = GameObject.FindWithTag ("Player").GetComponent<Transform>();
}
}
}
/// <summary>
/// Update is called once per frame.
/// </summary>
void Update () {
if (target == null)
return;
// Face the target.
transform.LookAt(target);
// Get the distance between the chaser and the target.
float distance = Vector3.Distance(transform.position,target.position);
// So long as the chaser is farther away than the minimum distance, move towards it at rate speed.
if(distance > minDist)
transform.position += transform.forward * speed * Time.deltaTime;
}
/// <summary>
/// Set the target of the chaser.
/// </summary>
public void SetTarget(Transform newTarget) {
target = newTarget;
}
}