Documentation for the Unity C# Library
Loading...
Searching...
No Matches
NearWaypoint.cs
Go to the documentation of this file.
1using PixoVR.Core;
2using UnityEngine;
3
4namespace PixoVR.Event
5{
7 {
8 public Transform RespawnTransform { get; set; }
9 }
10
11 public class NearWaypoint : MonoBehaviour
12 {
13 [SerializeField] private GameObject[] waypoints;
14 private Transform _cameraTransform;
15
16 private void Awake()
17 {
18 _cameraTransform = Camera.main.transform;
19 }
20
21 private void OnEnable()
22 {
23 // CheckNearestPoint();
24 }
25
26 public void CheckNearestPoint()
27 {
28 float min = 9999;
29 int index = 0;
30 for (int i = 0; i < waypoints.Length; i++)
31 {
32 if (Vector3.Distance(_cameraTransform.position, waypoints[i].transform.position) < min
33 && waypoints[i].activeSelf)
34 {
35 index = i;
36 min = Vector3.Distance(_cameraTransform.position, waypoints[i].transform.position);
37 }
38 }
39 RespawntoWaypoint(index);
40 }
41
42 private void RespawntoWaypoint(int index)
43 {
44 transform.SetPositionAndRotation(waypoints[index].transform.position,
45 waypoints[index].transform.rotation);
46
48 {
49 RespawnTransform = transform
50 });
51 }
52 }
53}
Intentionally made partial, in case you want to extend it easily.
GameObject[] waypoints
[SerializeField]
void RespawntoWaypoint(int index)