OVRPlayerController.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. using System;
  2. using UnityEngine;
  3. [RequireComponent(typeof(CharacterController))]
  4. public class OVRPlayerController : MonoBehaviour
  5. {
  6. private void Start()
  7. {
  8. Vector3 localPosition = this.CameraRig.transform.localPosition;
  9. localPosition.z = OVRManager.profile.eyeDepth;
  10. this.CameraRig.transform.localPosition = localPosition;
  11. }
  12. private void Awake()
  13. {
  14. this.Controller = base.gameObject.GetComponent<CharacterController>();
  15. if (this.Controller == null)
  16. {
  17. Debug.LogWarning("OVRPlayerController: No CharacterController attached.");
  18. }
  19. OVRCameraRig[] componentsInChildren = base.gameObject.GetComponentsInChildren<OVRCameraRig>();
  20. if (componentsInChildren.Length == 0)
  21. {
  22. Debug.LogWarning("OVRPlayerController: No OVRCameraRig attached.");
  23. }
  24. else if (componentsInChildren.Length > 1)
  25. {
  26. Debug.LogWarning("OVRPlayerController: More then 1 OVRCameraRig attached.");
  27. }
  28. else
  29. {
  30. this.CameraRig = componentsInChildren[0];
  31. }
  32. this.InitialYRotation = base.transform.rotation.eulerAngles.y;
  33. }
  34. private void OnEnable()
  35. {
  36. OVRManager.display.RecenteredPose += this.ResetOrientation;
  37. if (this.CameraRig != null)
  38. {
  39. this.CameraRig.UpdatedAnchors += this.UpdateTransform;
  40. }
  41. }
  42. private void OnDisable()
  43. {
  44. OVRManager.display.RecenteredPose -= this.ResetOrientation;
  45. if (this.CameraRig != null)
  46. {
  47. this.CameraRig.UpdatedAnchors -= this.UpdateTransform;
  48. }
  49. }
  50. protected virtual void Update()
  51. {
  52. if (this.useProfileData)
  53. {
  54. OVRPose? initialPose = this.InitialPose;
  55. if (initialPose == null)
  56. {
  57. this.InitialPose = new OVRPose?(new OVRPose
  58. {
  59. position = this.CameraRig.transform.localPosition,
  60. orientation = this.CameraRig.transform.localRotation
  61. });
  62. }
  63. Vector3 localPosition = this.CameraRig.transform.localPosition;
  64. if (OVRManager.instance.trackingOriginType == OVRManager.TrackingOrigin.EyeLevel)
  65. {
  66. localPosition.y = OVRManager.profile.eyeHeight - 0.5f * this.Controller.height + this.Controller.center.y;
  67. }
  68. else if (OVRManager.instance.trackingOriginType == OVRManager.TrackingOrigin.FloorLevel)
  69. {
  70. localPosition.y = -(0.5f * this.Controller.height) + this.Controller.center.y;
  71. }
  72. this.CameraRig.transform.localPosition = localPosition;
  73. }
  74. else
  75. {
  76. OVRPose? initialPose2 = this.InitialPose;
  77. if (initialPose2 != null)
  78. {
  79. this.CameraRig.transform.localPosition = this.InitialPose.Value.position;
  80. this.CameraRig.transform.localRotation = this.InitialPose.Value.orientation;
  81. this.InitialPose = null;
  82. }
  83. }
  84. this.UpdateMovement();
  85. Vector3 vector = Vector3.zero;
  86. float num = 1f + this.Damping * this.SimulationRate * Time.deltaTime;
  87. this.MoveThrottle.x = this.MoveThrottle.x / num;
  88. this.MoveThrottle.y = ((this.MoveThrottle.y <= 0f) ? this.MoveThrottle.y : (this.MoveThrottle.y / num));
  89. this.MoveThrottle.z = this.MoveThrottle.z / num;
  90. vector += this.MoveThrottle * this.SimulationRate * Time.deltaTime;
  91. if (this.Controller.isGrounded && this.FallSpeed <= 0f)
  92. {
  93. this.FallSpeed = Physics.gravity.y * (this.GravityModifier * 0.002f);
  94. }
  95. else
  96. {
  97. this.FallSpeed += Physics.gravity.y * (this.GravityModifier * 0.002f) * this.SimulationRate * Time.deltaTime;
  98. }
  99. vector.y += this.FallSpeed * this.SimulationRate * Time.deltaTime;
  100. if (this.Controller.isGrounded && this.MoveThrottle.y <= base.transform.lossyScale.y * 0.001f)
  101. {
  102. float stepOffset = this.Controller.stepOffset;
  103. Vector3 vector2 = new Vector3(vector.x, 0f, vector.z);
  104. float d = Mathf.Max(stepOffset, vector2.magnitude);
  105. vector -= d * Vector3.up;
  106. }
  107. Vector3 vector3 = Vector3.Scale(this.Controller.transform.localPosition + vector, new Vector3(1f, 0f, 1f));
  108. this.Controller.Move(vector);
  109. Vector3 vector4 = Vector3.Scale(this.Controller.transform.localPosition, new Vector3(1f, 0f, 1f));
  110. if (vector3 != vector4)
  111. {
  112. this.MoveThrottle += (vector4 - vector3) / (this.SimulationRate * Time.deltaTime);
  113. }
  114. }
  115. public virtual void UpdateMovement()
  116. {
  117. if (this.HaltUpdateMovement)
  118. {
  119. return;
  120. }
  121. bool flag = Input.GetKey(KeyCode.W) || Input.GetKey(KeyCode.UpArrow);
  122. bool flag2 = Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow);
  123. bool flag3 = Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow);
  124. bool flag4 = Input.GetKey(KeyCode.S) || Input.GetKey(KeyCode.DownArrow);
  125. bool flag5 = false;
  126. if (OVRInput.Get(OVRInput.Button.DpadUp, OVRInput.Controller.Active))
  127. {
  128. flag = true;
  129. flag5 = true;
  130. }
  131. if (OVRInput.Get(OVRInput.Button.DpadDown, OVRInput.Controller.Active))
  132. {
  133. flag4 = true;
  134. flag5 = true;
  135. }
  136. this.MoveScale = 1f;
  137. if ((flag && flag2) || (flag && flag3) || (flag4 && flag2) || (flag4 && flag3))
  138. {
  139. this.MoveScale = 0.707106769f;
  140. }
  141. if (!this.Controller.isGrounded)
  142. {
  143. this.MoveScale = 0f;
  144. }
  145. this.MoveScale *= this.SimulationRate * Time.deltaTime;
  146. float num = this.Acceleration * 0.1f * this.MoveScale * this.MoveScaleMultiplier;
  147. if (flag5 || Input.GetKey(KeyCode.LeftShift) || Input.GetKey(KeyCode.RightShift))
  148. {
  149. num *= 2f;
  150. }
  151. Vector3 eulerAngles = base.transform.rotation.eulerAngles;
  152. eulerAngles.z = (eulerAngles.x = 0f);
  153. Quaternion rotation = Quaternion.Euler(eulerAngles);
  154. if (flag)
  155. {
  156. this.MoveThrottle += rotation * (base.transform.lossyScale.z * num * Vector3.forward);
  157. }
  158. if (flag4)
  159. {
  160. this.MoveThrottle += rotation * (base.transform.lossyScale.z * num * this.BackAndSideDampen * Vector3.back);
  161. }
  162. if (flag2)
  163. {
  164. this.MoveThrottle += rotation * (base.transform.lossyScale.x * num * this.BackAndSideDampen * Vector3.left);
  165. }
  166. if (flag3)
  167. {
  168. this.MoveThrottle += rotation * (base.transform.lossyScale.x * num * this.BackAndSideDampen * Vector3.right);
  169. }
  170. Vector3 eulerAngles2 = base.transform.rotation.eulerAngles;
  171. bool flag6 = OVRInput.Get(OVRInput.Button.PrimaryShoulder, OVRInput.Controller.Active);
  172. if (flag6 && !this.prevHatLeft)
  173. {
  174. eulerAngles2.y -= this.RotationRatchet;
  175. }
  176. this.prevHatLeft = flag6;
  177. bool flag7 = OVRInput.Get(OVRInput.Button.SecondaryShoulder, OVRInput.Controller.Active);
  178. if (flag7 && !this.prevHatRight)
  179. {
  180. eulerAngles2.y += this.RotationRatchet;
  181. }
  182. this.prevHatRight = flag7;
  183. if (Input.GetKeyDown(KeyCode.Q))
  184. {
  185. eulerAngles2.y -= this.RotationRatchet;
  186. }
  187. if (Input.GetKeyDown(KeyCode.E))
  188. {
  189. eulerAngles2.y += this.RotationRatchet;
  190. }
  191. float num2 = this.SimulationRate * Time.deltaTime * this.RotationAmount * this.RotationScaleMultiplier;
  192. if (!this.SkipMouseRotation)
  193. {
  194. eulerAngles2.y += Input.GetAxis("Mouse X") * num2 * 3.25f;
  195. }
  196. num = this.Acceleration * 0.1f * this.MoveScale * this.MoveScaleMultiplier;
  197. num *= 1f + OVRInput.Get(OVRInput.Axis1D.PrimaryIndexTrigger, OVRInput.Controller.Active);
  198. Vector2 vector = OVRInput.Get(OVRInput.Axis2D.PrimaryThumbstick, OVRInput.Controller.Active);
  199. if (vector.y > 0f)
  200. {
  201. this.MoveThrottle += rotation * (vector.y * base.transform.lossyScale.z * num * Vector3.forward);
  202. }
  203. if (vector.y < 0f)
  204. {
  205. this.MoveThrottle += rotation * (Mathf.Abs(vector.y) * base.transform.lossyScale.z * num * this.BackAndSideDampen * Vector3.back);
  206. }
  207. if (vector.x < 0f)
  208. {
  209. this.MoveThrottle += rotation * (Mathf.Abs(vector.x) * base.transform.lossyScale.x * num * this.BackAndSideDampen * Vector3.left);
  210. }
  211. if (vector.x > 0f)
  212. {
  213. this.MoveThrottle += rotation * (vector.x * base.transform.lossyScale.x * num * this.BackAndSideDampen * Vector3.right);
  214. }
  215. Vector2 vector2 = OVRInput.Get(OVRInput.Axis2D.SecondaryThumbstick, OVRInput.Controller.Active);
  216. eulerAngles2.y += vector2.x * num2;
  217. base.transform.rotation = Quaternion.Euler(eulerAngles2);
  218. }
  219. public void UpdateTransform(OVRCameraRig rig)
  220. {
  221. Transform trackingSpace = this.CameraRig.trackingSpace;
  222. Transform centerEyeAnchor = this.CameraRig.centerEyeAnchor;
  223. if (this.HmdRotatesY)
  224. {
  225. Vector3 position = trackingSpace.position;
  226. Quaternion rotation = trackingSpace.rotation;
  227. base.transform.rotation = Quaternion.Euler(0f, centerEyeAnchor.rotation.eulerAngles.y, 0f);
  228. trackingSpace.position = position;
  229. trackingSpace.rotation = rotation;
  230. }
  231. }
  232. public bool Jump()
  233. {
  234. if (!this.Controller.isGrounded)
  235. {
  236. return false;
  237. }
  238. this.MoveThrottle += new Vector3(0f, base.transform.lossyScale.y * this.JumpForce, 0f);
  239. return true;
  240. }
  241. public void Stop()
  242. {
  243. this.Controller.Move(Vector3.zero);
  244. this.MoveThrottle = Vector3.zero;
  245. this.FallSpeed = 0f;
  246. }
  247. public void GetMoveScaleMultiplier(ref float moveScaleMultiplier)
  248. {
  249. moveScaleMultiplier = this.MoveScaleMultiplier;
  250. }
  251. public void SetMoveScaleMultiplier(float moveScaleMultiplier)
  252. {
  253. this.MoveScaleMultiplier = moveScaleMultiplier;
  254. }
  255. public void GetRotationScaleMultiplier(ref float rotationScaleMultiplier)
  256. {
  257. rotationScaleMultiplier = this.RotationScaleMultiplier;
  258. }
  259. public void SetRotationScaleMultiplier(float rotationScaleMultiplier)
  260. {
  261. this.RotationScaleMultiplier = rotationScaleMultiplier;
  262. }
  263. public void GetSkipMouseRotation(ref bool skipMouseRotation)
  264. {
  265. skipMouseRotation = this.SkipMouseRotation;
  266. }
  267. public void SetSkipMouseRotation(bool skipMouseRotation)
  268. {
  269. this.SkipMouseRotation = skipMouseRotation;
  270. }
  271. public void GetHaltUpdateMovement(ref bool haltUpdateMovement)
  272. {
  273. haltUpdateMovement = this.HaltUpdateMovement;
  274. }
  275. public void SetHaltUpdateMovement(bool haltUpdateMovement)
  276. {
  277. this.HaltUpdateMovement = haltUpdateMovement;
  278. }
  279. public void ResetOrientation()
  280. {
  281. if (this.HmdResetsY)
  282. {
  283. Vector3 eulerAngles = base.transform.rotation.eulerAngles;
  284. eulerAngles.y = this.InitialYRotation;
  285. base.transform.rotation = Quaternion.Euler(eulerAngles);
  286. }
  287. }
  288. public float Acceleration = 0.1f;
  289. public float Damping = 0.3f;
  290. public float BackAndSideDampen = 0.5f;
  291. public float JumpForce = 0.3f;
  292. public float RotationAmount = 1.5f;
  293. public float RotationRatchet = 45f;
  294. public bool HmdResetsY = true;
  295. public bool HmdRotatesY = true;
  296. public float GravityModifier = 0.379f;
  297. public bool useProfileData = true;
  298. protected CharacterController Controller;
  299. protected OVRCameraRig CameraRig;
  300. private float MoveScale = 1f;
  301. private Vector3 MoveThrottle = Vector3.zero;
  302. private float FallSpeed;
  303. private OVRPose? InitialPose;
  304. private float InitialYRotation;
  305. private float MoveScaleMultiplier = 1f;
  306. private float RotationScaleMultiplier = 1f;
  307. private bool SkipMouseRotation;
  308. private bool HaltUpdateMovement;
  309. private bool prevHatLeft;
  310. private bool prevHatRight;
  311. private float SimulationRate = 60f;
  312. }