-
Notifications
You must be signed in to change notification settings - Fork 0
/
Waypoints.cs
40 lines (35 loc) · 1017 Bytes
/
Waypoints.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Waypoints : MonoBehaviour {
public GameObject[] waypoints;
public GameObject player;
int current = 0;
public float speed;
float WPradius = 1;
void Update () {
if(Vector3.Distance(waypoints[current].transform.position, transform.position) < WPradius)
{
current = Random.Range(0,waypoints.Length);
if (current >= waypoints.Length)
{
current = 0;
}
}
transform.position = Vector3.MoveTowards(transform.position, waypoints[current].transform.position, Time.deltaTime * speed);
}
void OnTriggerEnter(Collider n)
{
if (n.gameObject == player)
{
player.transform.parent = transform;
}
}
void OnTriggerExit(Collider n)
{
if (n.gameObject == player)
{
player.transform.parent = null;
}
}
}