-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChaser.cs
48 lines (37 loc) · 1.04 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
using UnityEngine;
using System.Collections;
//[RequireComponent(typeof(CharacterController))]
public class Chaser : MonoBehaviour {
public float speed = 50.0f;
public float minDist = 0.5f;
public Transform target;
// Use this for initialization
void Start ()
{
// if no target specified, assume the player
if (target == null) {
if (GameObject.FindWithTag ("Claw")!=null)
{
target = GameObject.FindWithTag ("Claw").GetComponent<Transform>();
}
}
}
// Update is called once per frame
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;
}
// Set the target of the chaser
public void SetTarget(Transform newTarget)
{
target = newTarget;
}
}