DevCameraController.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. using UnityEngine;
  2. using UnityEngine.Serialization;
  3. namespace ARLocation.Utils
  4. {
  5. public class DevCameraController : MonoBehaviour
  6. {
  7. /// <summary>
  8. /// The mouse look/rotation sensitivity.
  9. /// </summary>
  10. public float MouseSensitivity = 1.0f;
  11. /// <summary>
  12. /// The walking speed
  13. /// </summary>
  14. [FormerlySerializedAs("speed")] public float Speed = 1.0f;
  15. // Current orientation parameters
  16. float rotationY;
  17. float rotationX;
  18. // The initial location
  19. Location firstLocation;
  20. // The accumulated lat/lng displacement
  21. private Vector3 accumDelta;
  22. // Use this for initialization
  23. void Awake()
  24. {
  25. // If we are not running on a device, make this the main
  26. // camera, else, self-destruct.
  27. if (!Misc.IsARDevice())
  28. {
  29. var arCamera = GameObject.Find("AR Camera");
  30. if (arCamera != null)
  31. {
  32. arCamera.tag = "Untagged";
  33. arCamera.SetActive(false);
  34. }
  35. GetComponent<Camera>().gameObject.SetActive(true);
  36. GameObject o;
  37. (o = gameObject).AddComponent<AudioListener>();
  38. o.tag = "MainCamera";
  39. ARLocationManager.Instance.WaitForARTrackingToStart = false;
  40. }
  41. else
  42. {
  43. Destroy(gameObject);
  44. }
  45. var rotation = transform.rotation;
  46. rotationX = rotation.eulerAngles.x;
  47. rotationY = rotation.eulerAngles.y;
  48. }
  49. // Update is called once per frame
  50. void Update()
  51. {
  52. var forward = Vector3.ProjectOnPlane(transform.forward, new Vector3(0, 1, 0));
  53. var initialPosition = transform.position;
  54. var spd = Speed;
  55. if (Input.GetKey(KeyCode.LeftShift))
  56. {
  57. spd *= 2;
  58. }
  59. if (Input.GetKey("w"))
  60. {
  61. transform.Translate(
  62. forward * spd, Space.World
  63. );
  64. }
  65. if (Input.GetKey("s"))
  66. {
  67. transform.Translate(
  68. -forward * spd, Space.World
  69. );
  70. }
  71. if (Input.GetKey("d"))
  72. {
  73. transform.Translate(
  74. transform.right * spd, Space.World
  75. );
  76. }
  77. if (Input.GetKey("a"))
  78. {
  79. transform.Translate(
  80. -transform.right * spd, Space.World
  81. );
  82. }
  83. if (Input.GetKey("up"))
  84. {
  85. transform.Translate(
  86. transform.up * spd, Space.World
  87. );
  88. }
  89. var finalPosition = transform.position;
  90. var delta = finalPosition - initialPosition;
  91. var locMngr = ARLocationProvider.Instance;
  92. if (firstLocation == null)
  93. {
  94. firstLocation = locMngr.CurrentLocation.ToLocation();
  95. }
  96. accumDelta += delta * 0.00001f;
  97. //locMngr.UpdateMockLocation(new Location(
  98. // firstLocation.latitude + accumDelta.z,
  99. // firstLocation.longitude + accumDelta.x,
  100. // 0
  101. //));
  102. rotationY += Input.GetAxis("Mouse X") * MouseSensitivity;
  103. rotationX -= Input.GetAxis("Mouse Y") * MouseSensitivity;
  104. transform.localRotation = Quaternion.Euler(rotationX, rotationY, 0);
  105. }
  106. }
  107. }