94 lines
2.4 KiB
C#
94 lines
2.4 KiB
C#
using Photon.Pun;
|
|
using Photon.VR;
|
|
using Photon.VR.Player;
|
|
using UnityEngine;
|
|
|
|
namespace BlockSpace.Worlds
|
|
{
|
|
public static class PlayerTeleportUtil
|
|
{
|
|
public static Transform GetLocalVrTeleportRoot()
|
|
{
|
|
if (PhotonVRManager.Manager == null || PhotonVRManager.Manager.Head == null)
|
|
{
|
|
return null;
|
|
}
|
|
Transform head = PhotonVRManager.Manager.Head;
|
|
if (head.parent == null)
|
|
{
|
|
return head;
|
|
}
|
|
Transform parent = head.parent;
|
|
while (parent != null)
|
|
{
|
|
if (parent.GetComponent<CharacterController>() != null)
|
|
{
|
|
return parent;
|
|
}
|
|
if (parent.parent == null)
|
|
{
|
|
return parent;
|
|
}
|
|
parent = parent.parent;
|
|
}
|
|
return head;
|
|
}
|
|
|
|
public static bool TryTeleportLocalPlayer(Vector3 position)
|
|
{
|
|
Transform localVrTeleportRoot = GetLocalVrTeleportRoot();
|
|
if (localVrTeleportRoot != null)
|
|
{
|
|
TeleportAndZeroVelocity(localVrTeleportRoot.gameObject, position);
|
|
return true;
|
|
}
|
|
PhotonVRPlayer photonVRPlayer = ((PhotonVRManager.Manager != null) ? PhotonVRManager.Manager.LocalPlayer : null);
|
|
if (photonVRPlayer != null)
|
|
{
|
|
TeleportAndZeroVelocity(photonVRPlayer.gameObject, position);
|
|
return true;
|
|
}
|
|
int num = ((PhotonNetwork.LocalPlayer != null) ? PhotonNetwork.LocalPlayer.ActorNumber : 0);
|
|
PhotonVRPlayer[] array = Object.FindObjectsByType<PhotonVRPlayer>(FindObjectsInactive.Include);
|
|
foreach (PhotonVRPlayer photonVRPlayer2 in array)
|
|
{
|
|
if (!(photonVRPlayer2 == null))
|
|
{
|
|
PhotonView component = photonVRPlayer2.GetComponent<PhotonView>();
|
|
if (!(component == null) && (num <= 0 || component.OwnerActorNr == num) && (num > 0 || component.IsMine))
|
|
{
|
|
TeleportAndZeroVelocity(photonVRPlayer2.gameObject, position);
|
|
return true;
|
|
}
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
private static void TeleportAndZeroVelocity(GameObject root, Vector3 position)
|
|
{
|
|
if (!(root == null))
|
|
{
|
|
CharacterController component = root.GetComponent<CharacterController>();
|
|
if (component != null)
|
|
{
|
|
bool enabled = component.enabled;
|
|
component.enabled = false;
|
|
root.transform.position = position;
|
|
component.enabled = enabled;
|
|
}
|
|
else
|
|
{
|
|
root.transform.position = position;
|
|
}
|
|
Rigidbody component2 = root.GetComponent<Rigidbody>();
|
|
if (component2 != null)
|
|
{
|
|
component2.linearVelocity = Vector3.zero;
|
|
component2.angularVelocity = Vector3.zero;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|