-
Notifications
You must be signed in to change notification settings - Fork 461
Expand file tree
/
Copy pathCollideTeleporter.cs
More file actions
57 lines (50 loc) · 1.79 KB
/
CollideTeleporter.cs
File metadata and controls
57 lines (50 loc) · 1.79 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
using UnityEngine;
using Unity.Netcode.Components;
using Unity.Netcode;
public class CollideTeleporter : MonoBehaviour
{
[Tooltip("The destination GameObject transform. All rotation and scale will be applied, but position values can be ignored by setting the Preserve properties.")]
public GameObject Destination;
[Tooltip("When checked, the x-axis position value will remain the same during teleporting.")]
public bool Preserve_XAxis;
[Tooltip("When checked, the y-axis position value will remain the same during teleporting.")]
public bool Preserve_YAxis;
[Tooltip("When checked, the z-axis position value will remain the same during teleporting.")]
public bool Preserve_ZAxis;
private void OnCollisionEnter(Collision collision)
{
if (NetworkManager.Singleton == null || !NetworkManager.Singleton.IsListening)
{
return;
}
var playerMover = collision.gameObject.GetComponent<PlayerMovement>();
if (playerMover == null || playerMover.IsTeleporting)
{
return;
}
var networkTransform = collision.gameObject.GetComponent<NetworkTransform>();
if (networkTransform == null || Destination == null)
{
return;
}
var objectTransform = networkTransform.transform;
var position = Destination.transform.position;
if (Preserve_XAxis)
{
position.x = objectTransform.position.x;
}
if (Preserve_YAxis)
{
position.y = objectTransform.position.y;
}
else
{
position.y = transform.position.y + 0.1f;
}
if (Preserve_ZAxis)
{
position.z = objectTransform.position.z;
}
playerMover.Telporting(position);
}
}