forked from mouseroot/Unity3D-Scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathInstantPathMovement.cs
113 lines (98 loc) · 3.25 KB
/
InstantPathMovement.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
using UnityEngine;
using UnityEditor;
using System;
using System.Collections;
[Serializable]
public class PathNode
{
public string nodeName;
public Vector3 pathPosition;
public float delayTime = 0.1f;
}
public class InstantPathMovement : MonoBehaviour {
public float startTimer;
public bool lockObjectToPath = true;
public bool playOnce = true;
public PathNode[] pathNodes;
private Vector3 originalPosition;
private int currentPathIndex = 0;
// Use this for initialization
void Start () {
originalPosition = transform.position;
Invoke("nextNode", startTimer);
}
// Update is called once per frame
void Update () {
}
public void nextNode()
{
Vector3 position;
float transitionTimer;
if (currentPathIndex > pathNodes.Length - 1)
{
if (playOnce)
{
currentPathIndex = pathNodes.Length - 1;
}
else
{
currentPathIndex = 0;
}
}
position = pathNodes[currentPathIndex].pathPosition;
transitionTimer = pathNodes[currentPathIndex].delayTime;
transform.position = position;
currentPathIndex++;
Invoke("nextNode", transitionTimer);
}
public void OnDrawGizmosSelected()
{
if (lockObjectToPath && Application.isEditor && Application.isPlaying == false)
{
transform.position = pathNodes[0].pathPosition;
}
for (int i = 0; i < pathNodes.Length;i++ )
{
//If first node
if (i == 0)
{
Gizmos.color = Color.green;
Gizmos.DrawSphere(pathNodes[0].pathPosition, 0.06f);
Gizmos.color = Color.white;
Gizmos.DrawLine(pathNodes[0].pathPosition, pathNodes[i].pathPosition);
Handles.color = Color.red;
if (pathNodes[i].nodeName == null || pathNodes[i].nodeName == "")
{
Handles.Label(pathNodes[0].pathPosition, "Node " + i + "(" + pathNodes[0].delayTime + "s)");
}
else
{
Handles.Label(pathNodes[0].pathPosition, pathNodes[i].nodeName + " (" + pathNodes[0].delayTime + "s)");
}
}
//if node is not the first
else
{
if (i == pathNodes.Length - 1)
{
Gizmos.color = Color.red;
}
else
{
Gizmos.color = Color.blue;
}
Gizmos.DrawSphere(pathNodes[i].pathPosition, 0.05f);
Gizmos.color = Color.white;
Gizmos.DrawLine(pathNodes[i - 1].pathPosition, pathNodes[i].pathPosition);
if (pathNodes[i].nodeName == null || pathNodes[i].nodeName == "")
{
Handles.Label(pathNodes[i].pathPosition, "Node " + i + "(" + pathNodes[i].delayTime + "s)");
}
else
{
Handles.Label(pathNodes[i].pathPosition, pathNodes[i].nodeName + " (" + pathNodes[i].delayTime + "s)");
}
}
}
}
}