PlayerController.cs 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. public class PlayerController : MonoBehaviour
  6. {
  7. public Transform camera;
  8. public Vector3 position;
  9. public Vector3 currentposition;
  10. public Quaternion rotation;
  11. public static CharacterController character;
  12. public Transform mapIcon;
  13. public float moveSpeed;
  14. public float jumpSpeed;
  15. private float horizontalMove, verticalMove;
  16. public Vector3 dir;
  17. public static float gravity;
  18. public float g;
  19. private Vector3 velocity;
  20. public Button walk_or_fly;
  21. public Transform groundCheck;
  22. public float checkRadius;
  23. public LayerMask groundLayer;
  24. public bool isGround;
  25. // Start is called before the first frame update
  26. //public bool isWalk = true;
  27. public float chight;
  28. public float phight;
  29. void Start()
  30. {
  31. character = GetComponent<CharacterController>();
  32. position = character.transform.position;
  33. rotation = character.transform.rotation;
  34. chight = camera.transform.position.y;
  35. phight = character.transform.position.y;
  36. }
  37. // Update is called once per frame
  38. void Update()
  39. {
  40. if (ButtonsController.isWalk)
  41. {
  42. isGround = Physics.CheckSphere(groundCheck.position, checkRadius, groundLayer);
  43. if (isGround && velocity.y < 0)
  44. {
  45. velocity = new Vector3(0, 0, 0);
  46. }
  47. else
  48. {
  49. if (gravity == 0)
  50. {
  51. velocity = new Vector3(0, 0, 0);
  52. }
  53. }
  54. horizontalMove = Input.GetAxis("Horizontal") * moveSpeed;
  55. verticalMove = Input.GetAxis("Vertical") * moveSpeed;
  56. dir = transform.forward * verticalMove + transform.right * horizontalMove;
  57. if (Input.GetKey(KeyCode.LeftShift))
  58. {
  59. dir *= 3f;
  60. }
  61. character.Move(dir * Time.deltaTime);
  62. if (Input.GetButtonDown("Jump") && isGround)
  63. {
  64. velocity.y = jumpSpeed;
  65. }
  66. if (Input.GetKeyDown(KeyCode.R))
  67. {
  68. Physics.autoSyncTransforms = true;
  69. character.transform.position = position;
  70. character.transform.rotation = rotation;
  71. }
  72. velocity.y -= gravity * Time.deltaTime;
  73. character.Move(velocity * Time.deltaTime);
  74. currentposition = character.transform.position;
  75. }
  76. else
  77. {
  78. Physics.autoSyncTransforms = true;
  79. character.transform.position = camera.position;
  80. character.transform.rotation = new Quaternion(0, camera.rotation.y, 0, camera.rotation.w);//*/camera.rotation;
  81. }
  82. g = gravity;
  83. }
  84. //***********************************场景切换******************************//
  85. /// <summary>
  86. /// 大门
  87. /// </summary>
  88. public void OnClickBtnGate()
  89. {
  90. ButtonsController.isWalk = true;
  91. Physics.autoSyncTransforms = true;
  92. character.transform.position = new Vector3(10.0f,2.00f,30.0f);
  93. character.transform.localEulerAngles = new Vector3(0, -180.0f, 0);
  94. camera.localRotation = Quaternion.Euler(340, 0, 0);
  95. }
  96. /// <summary>
  97. /// 进线区
  98. /// </summary>
  99. public void OnClickBtnIncoming()
  100. {
  101. ButtonsController.isWalk = true;
  102. Physics.autoSyncTransforms = true;
  103. character.transform.position = new Vector3(32.0f, 2.0f, 2.0f);
  104. character.transform.localEulerAngles = new Vector3(1.0f, -153.0f, 0);
  105. camera.localRotation = Quaternion.Euler(330, 360, 0);
  106. }
  107. /// <summary>
  108. /// 主变区
  109. /// </summary>
  110. public void OnClickBtnMaintransformer()
  111. {
  112. ButtonsController.isWalk = true;
  113. Physics.autoSyncTransforms = true;
  114. character.transform.position = new Vector3(8.0f, 2.0f, -10.0f);
  115. character.transform.localEulerAngles = new Vector3(0, -30.0f, 0);
  116. camera.localRotation = Quaternion.Euler(345, 360, 0);
  117. }
  118. /// <summary>
  119. /// 馈线区
  120. /// </summary>
  121. public void OnClickBtnFeeder()
  122. {
  123. ButtonsController.isWalk = true;
  124. Physics.autoSyncTransforms = true;
  125. character.transform.position = new Vector3(-27.0f, 2.0f, 20.0f);
  126. character.transform.localEulerAngles = new Vector3(0, -200, 0);
  127. camera.localRotation = Quaternion.Euler(0, 360, 0);
  128. }
  129. /// <summary>
  130. /// 控制室
  131. /// </summary>
  132. public void OnClickBtnControl()
  133. {
  134. ButtonsController.isWalk = true;
  135. Physics.autoSyncTransforms = true;
  136. character.transform.position = new Vector3(-14.95235f, 9.0f, -20.0f);
  137. character.transform.localEulerAngles = new Vector3(0, -360, 0);
  138. camera.localRotation = Quaternion.Euler(350, 0, 0);
  139. }
  140. /// <summary>
  141. /// 电容室
  142. /// </summary>
  143. public void OnClickBtnCapacitance()
  144. {
  145. ButtonsController.isWalk = true;
  146. Physics.autoSyncTransforms = true;
  147. character.transform.position = new Vector3(-14.7f, 9.0f, 1.0f);
  148. character.transform.localEulerAngles = new Vector3(0, -360, 0);
  149. camera.localRotation = Quaternion.Euler(350, 0, 0);
  150. }
  151. /// <summary>
  152. /// 大楼屋顶
  153. /// </summary>
  154. public void OnClickBtnRoof()
  155. {
  156. ButtonsController.isWalk = true;
  157. Physics.autoSyncTransforms = true;
  158. character.transform.position = new Vector3(-12.0f, 16.0f, -20.0f);
  159. character.transform.localEulerAngles = new Vector3(0, -350.0f, 0);
  160. camera.localRotation = Quaternion.Euler(360, 0, 0);
  161. }
  162. /// <summary>
  163. /// 10kv高压室
  164. /// </summary>
  165. public void OnClickBtn10kv()
  166. {
  167. ButtonsController.isWalk = true;
  168. Physics.autoSyncTransforms = true;
  169. character.transform.position = new Vector3(12.0f, 2.0f, -24.0f);
  170. character.transform.localEulerAngles = new Vector3(0, -270, 0);
  171. camera.localRotation = Quaternion.Euler(350, 0, 0);
  172. }
  173. /// <summary>
  174. /// 27.5kv高压室
  175. /// </summary>
  176. public void OnClickBtn275kv()
  177. {
  178. ButtonsController.isWalk = true;
  179. Physics.autoSyncTransforms = true;
  180. character.transform.position = new Vector3(-15.0f,2.0f, -5.0f);
  181. character.transform.localEulerAngles = new Vector3(0, -360.0f, 0);
  182. camera.localRotation = Quaternion.Euler(360, 0, 0);
  183. }
  184. public void setFly()
  185. {
  186. gravity = 0;
  187. camera.localPosition = new Vector3(0, 0, 0);
  188. }
  189. public void setWalk()
  190. {
  191. gravity = 9.81f;
  192. camera.localPosition = new Vector3(0, 0, 0);
  193. }
  194. }