SteamVR_Teleporter.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System;
  2. using UnityEngine;
  3. public class SteamVR_Teleporter : MonoBehaviour
  4. {
  5. private Transform reference
  6. {
  7. get
  8. {
  9. SteamVR_Camera steamVR_Camera = SteamVR_Render.Top();
  10. return (!(steamVR_Camera != null)) ? null : steamVR_Camera.origin;
  11. }
  12. }
  13. private void Start()
  14. {
  15. SteamVR_TrackedController steamVR_TrackedController = base.GetComponent<SteamVR_TrackedController>();
  16. if (steamVR_TrackedController == null)
  17. {
  18. steamVR_TrackedController = base.gameObject.AddComponent<SteamVR_TrackedController>();
  19. }
  20. steamVR_TrackedController.TriggerClicked += this.DoClick;
  21. if (this.teleportType == SteamVR_Teleporter.TeleportType.TeleportTypeUseTerrain)
  22. {
  23. Transform reference = this.reference;
  24. if (reference != null)
  25. {
  26. reference.position = new Vector3(reference.position.x, Terrain.activeTerrain.SampleHeight(reference.position), reference.position.z);
  27. }
  28. }
  29. }
  30. private void DoClick(object sender, ClickedEventArgs e)
  31. {
  32. if (this.teleportOnClick)
  33. {
  34. Transform reference = this.reference;
  35. if (reference == null)
  36. {
  37. return;
  38. }
  39. float y = reference.position.y;
  40. Plane plane = new Plane(Vector3.up, -y);
  41. Ray ray = new Ray(base.transform.position, base.transform.forward);
  42. bool flag = false;
  43. float d = 0f;
  44. if (this.teleportType == SteamVR_Teleporter.TeleportType.TeleportTypeUseTerrain)
  45. {
  46. TerrainCollider component = Terrain.activeTerrain.GetComponent<TerrainCollider>();
  47. RaycastHit raycastHit;
  48. flag = component.Raycast(ray, out raycastHit, 1000f);
  49. d = raycastHit.distance;
  50. }
  51. else if (this.teleportType == SteamVR_Teleporter.TeleportType.TeleportTypeUseCollider)
  52. {
  53. RaycastHit raycastHit2;
  54. Physics.Raycast(ray, out raycastHit2);
  55. d = raycastHit2.distance;
  56. }
  57. else
  58. {
  59. flag = plane.Raycast(ray, out d);
  60. }
  61. if (flag)
  62. {
  63. Vector3 b = new Vector3(SteamVR_Render.Top().head.localPosition.x, 0f, SteamVR_Render.Top().head.localPosition.z);
  64. reference.position = ray.origin + ray.direction * d - new Vector3(reference.GetChild(0).localPosition.x, 0f, reference.GetChild(0).localPosition.z) - b;
  65. }
  66. }
  67. }
  68. public bool teleportOnClick;
  69. public SteamVR_Teleporter.TeleportType teleportType = SteamVR_Teleporter.TeleportType.TeleportTypeUseZeroY;
  70. public enum TeleportType
  71. {
  72. TeleportTypeUseTerrain,
  73. TeleportTypeUseCollider,
  74. TeleportTypeUseZeroY
  75. }
  76. }