-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeleportPlayer.cs
More file actions
261 lines (254 loc) · 9.41 KB
/
TeleportPlayer.cs
File metadata and controls
261 lines (254 loc) · 9.41 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
using UnityEngine;
using System.Collections;
public class TeleportPlayer : MonoBehaviour
{
//teleport instantly upon entering trigger?
public bool instantTeleport;
//teleport to a random pad?
private bool randomTeleport;
//teleport when a button is pressed?
private bool buttonTeleport;
//the name of the button
private string buttonName;
//teleport delayed?
private bool delayedTeleport;
//time it takes to teleport, if not instant
private float teleportTime = 3;
//only allow specific tag object? if left empty, any object can teleport
private string objectTag = "";
//one or more destination pads
public Transform[] dragYourDestinationObjectHere;
//height offset
public float teleportationHeightOffset = 1;
//a private float counting down the time
private float curTeleportTime;
//private bool checking if you entered the trigger
private bool inside;
//check to wait for arrived object to leave before enabling teleporation again
[HideInInspector]
public bool arrived;
//gameobjects inside the pad
private Transform subject;
//add a sound component if you want the teleport playing a sound when teleporting
public AudioSource teleportSound;
//add a sound component for the teleport pad, vibrating for example, or music if you want :D
//also to make it more apparent when the pad is off, stop this component playing with "teleportPadSound.Stop();"
//PS the distance is set to 10 units, so you only hear it standing close, not from the other side of the map
public AudioSource teleportPadSound;
//simple enable/disable function in case you want the teleport not working at some point
//without disabling the entire script, so receiving objects still works
private bool teleportPadOn = true;
void Start ()
{
//Set the countdown ready to the time you chose
curTeleportTime = teleportTime;
}
void Update ()
{
//check if theres something/someone inside
if(inside)
{
//if that object hasnt just arrived from another pad, teleport it
if(!arrived && teleportPadOn)
Teleport();
}
}
void Teleport()
{
//if you chose to teleport instantly
if(instantTeleport)
{
//if you chose instant + random teleport, teleport to a random pad from the array
if(randomTeleport)
{
//choose a random pad from the array
int chosenPad = Random.Range(0,dragYourDestinationObjectHere.Length);
//set arrived to true in that array, so it doesnt teleport the subject back
dragYourDestinationObjectHere[chosenPad].GetComponent<TeleportPlayer>().arrived = true;
//and teleport the subject
subject.transform.position = dragYourDestinationObjectHere[chosenPad].transform.position + new Vector3(0,teleportationHeightOffset,0);
//play teleport sound
teleportSound.Play();
}
else //if not random, teleport to the first one in the array list
{
//set arrived to true in that array, so it doesnt teleport the subject back
dragYourDestinationObjectHere[0].GetComponent<TeleportPlayer>().arrived = true;
//and teleport the subject
subject.transform.position = dragYourDestinationObjectHere[0].transform.position + new Vector3(0,teleportationHeightOffset,0);
//play teleport sound
teleportSound.Play();
}
}
else if(delayedTeleport) //if its a delayed teleport
{
//start the countdown
curTeleportTime-=1 * Time.deltaTime;
//if the countdown reaches zero
if(curTeleportTime <= 0)
{
//reset the countdown
curTeleportTime = teleportTime;
//if you chose delayed + random teleport, teleport to random pad from array, after the delay
if(randomTeleport)
{
//choose a random pad from the array
int chosenPad = Random.Range(0,dragYourDestinationObjectHere.Length);
//set arrived to true in that array, so it doesnt teleport the subject back
dragYourDestinationObjectHere[chosenPad].GetComponent<TeleportPlayer>().arrived = true;
//teleport
subject.transform.position = dragYourDestinationObjectHere[chosenPad].transform.position + new Vector3(0,teleportationHeightOffset,0);
//play teleport sound
teleportSound.Play();
}
else //if not random, teleport to the first one in the array list
{
//set arrived to true in that array, so it doesnt teleport the subject back
dragYourDestinationObjectHere[0].GetComponent<TeleportPlayer>().arrived = true;
//teleport
subject.transform.position = dragYourDestinationObjectHere[0].transform.position + new Vector3(0,teleportationHeightOffset,0);
//play teleport sound
teleportSound.Play();
}
}
}
else if(buttonTeleport) //if you selected a teleport activated by a button
{
//checks if the button you set in the inspector is being pressed
if(Input.GetButtonDown(buttonName))
{
if(delayedTeleport) //if you set it to button + delayed
{
//start the countdown
curTeleportTime-=1 * Time.deltaTime;
//if the countdown reaches zero
if(curTeleportTime <= 0)
{
//reset the countdown
curTeleportTime = teleportTime;
//if you chose delayed + random teleport + button, teleport to random pad from array, after the delay
// and button press
if(randomTeleport)
{
//choose a random pad from the array
int chosenPad = Random.Range(0,dragYourDestinationObjectHere.Length);
//set arrived to true in that array, so it doesnt teleport the subject back
dragYourDestinationObjectHere[chosenPad].GetComponent<TeleportPlayer>().arrived = true;
//teleport
subject.transform.position = dragYourDestinationObjectHere[chosenPad].transform.position + new Vector3(0,teleportationHeightOffset,0);
//play teleport sound
teleportSound.Play();
}
else //if not random, teleport to the first one in the array list
{
//set arrived to true in that array, so it doesnt teleport the subject back
dragYourDestinationObjectHere[0].GetComponent<TeleportPlayer>().arrived = true;
//teleport
subject.transform.position = dragYourDestinationObjectHere[0].transform.position + new Vector3(0,teleportationHeightOffset,0);
//play teleport sound
teleportSound.Play();
}
}
}
//if you chose button + random teleport, teleport to random pad from array, on button down
else if(randomTeleport)
{
//choose a random pad from the array
int chosenPad = Random.Range(0,dragYourDestinationObjectHere.Length);
//set arrived to true in that array, so it doesnt teleport the subject back
dragYourDestinationObjectHere[chosenPad].GetComponent<TeleportPlayer>().arrived = true;
//teleport
subject.transform.position = dragYourDestinationObjectHere[chosenPad].transform.position + new Vector3(0,teleportationHeightOffset,0);
//play teleport sound
teleportSound.Play();
}
else
{
//set arrived to true in that array, so it doesnt teleport the subject back
dragYourDestinationObjectHere[0].GetComponent<TeleportPlayer>().arrived = true;
//teleport
subject.transform.position = dragYourDestinationObjectHere[0].transform.position + new Vector3(0,teleportationHeightOffset,0);
//play teleport sound
teleportSound.Play();
}
}
}
}
void OnTriggerEnter(Collider trig)
{
//when an object enters the trigger
//if you set a tag in the inspector, check if an object has that tag
//otherwise the pad will take in and teleport any object
if(objectTag != "")
{
//if the objects tag is the same as the one allowed in the inspector
if(trig.gameObject.tag == objectTag)
{
//set the subject to be the entered object
subject = trig.transform;
//and check inside, ready for teleport
inside = true;
//if its a button teleport, the pad should be set to be ready to teleport again
//so the player/object doesn't have to leave the pad, and re enter again, we do that here
if(buttonTeleport)
{
arrived = false;
}
}
}
else
{
//set the subject to be the entered object
subject = trig.transform;
//and check inside, ready for teleport
inside = true;
//if its a button teleport, the pad should be set to be ready to teleport again
//so the player/object doesn't have to leave the pad, and re enter again, we do that here
if(buttonTeleport)
{
arrived = false;
}
}
}
void OnTriggerExit(Collider trig)
{
//////////////if you set a tag for the entering pad, you should also set it for the exiting pad////////
//when an object exists the trigger
//if you set a tag in the inspector, check if an object has that tag
//otherwise the pad will register any object as the one leaving
if(objectTag != "")
{
//if the objects tag is the same as the one allowed in the inspector
if(trig.gameObject.tag == objectTag)
{
//set that the object left
inside = false;
//reset countdown time
curTeleportTime = teleportTime;
//if the object that left the trigger is the same object as the subject
if(trig.transform == subject)
{
//set arrived to false, so that the pad can be used again
arrived = false;
}
//remove the subject from the pads memory
subject = null;
}
}
else //if tags arent important
{
//set that the object left
inside = false;
//reset countdown time
curTeleportTime = teleportTime;
//if the object that left the trigger is the same object as the subject
if(trig.transform == subject)
{
//set arrived to false, so that the pad can be used again
arrived = false;
}
//remove the subject from the pads memory
subject = null;
}
}
}