Advertisement
Guest User

Untitled

a guest
Nov 20th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.71 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.SceneManagement;
  3. using Mirror;
  4.  
  5. [RequireComponent(typeof(CharacterController))]
  6. public class PlayerController : NetworkBehaviour
  7. {
  8. public override void OnStartServer()
  9. {
  10. base.OnStartServer();
  11. playerColor = Random.ColorHSV(0f, 1f, 1f, 1f, 0.5f, 1f);
  12. }
  13.  
  14. [SyncVar(hook = nameof(SetColor))]
  15. Color playerColor = Color.black;
  16.  
  17. // Unity clones the material when GetComponent<Renderer>().material is called
  18. // Cache it here and destroy it in OnDestroy to prevent a memory leak
  19. Material cachedMaterial;
  20.  
  21. void SetColor(Color color)
  22. {
  23. if (cachedMaterial == null) cachedMaterial = GetComponent<Renderer>().material;
  24. cachedMaterial.color = color;
  25. }
  26.  
  27. void OnDisable()
  28. {
  29. if (isLocalPlayer)
  30. {
  31. Camera.main.transform.SetParent(null);
  32. Camera.main.transform.localPosition = new Vector3(0f, 50f, 0f);
  33. Camera.main.transform.localEulerAngles = new Vector3(90f, 0f, 0f);
  34. }
  35. }
  36.  
  37. void OnDestroy()
  38. {
  39. Destroy(cachedMaterial);
  40. }
  41.  
  42. CharacterController characterController;
  43.  
  44. public override void OnStartLocalPlayer()
  45. {
  46. base.OnStartLocalPlayer();
  47.  
  48. characterController = GetComponent<CharacterController>();
  49.  
  50. Camera.main.transform.SetParent(transform);
  51. Camera.main.transform.localPosition = new Vector3(0f, 3f, -8f);
  52. Camera.main.transform.localEulerAngles = new Vector3(10f, 0f, 0f);
  53.  
  54. }
  55.  
  56. [Command]
  57. public void CmdChangeScene(string newSceneName)
  58. {
  59. // get the client's player object
  60. GameObject player = connectionToClient.identity.gameObject;
  61.  
  62. Scene oldScene = player.scene;
  63. Scene newScene = SceneManager.GetSceneByName(newSceneName);
  64.  
  65. GameObject[] gameObjects = newScene.GetRootGameObjects();
  66. foreach(GameObject g in gameObjects)
  67. {
  68. SpawnHandler SHandler = g.GetComponent<SpawnHandler>();
  69. if (SHandler != null)
  70. {
  71. player.transform.position = SHandler.SpawnThePlayer();
  72. }
  73. }
  74.  
  75. // Move Player's gameobject into subScene
  76. SceneManager.MoveGameObjectToScene(player, newScene);
  77.  
  78. // Rebuild Observers for all networked objects
  79. foreach (NetworkIdentity networkIdentity in NetworkIdentity.spawned.Values)
  80. {
  81. Scene currScene = networkIdentity.gameObject.scene;
  82. if (currScene == newScene || currScene == oldScene)
  83. networkIdentity.RebuildObservers(false);
  84. }
  85. }
  86. [Header("Movement Settings")]
  87. public float moveSpeed = 8f;
  88. public float turnSpeedAccel = 5f;
  89. public float turnSpeedDecel = 5f;
  90. public float maxTurnSpeed = 150f;
  91.  
  92. [Header("Jump Settings")]
  93. public float jumpSpeed = 0f;
  94. public float jumpFactor = .025F;
  95.  
  96. [Header("Diagnostics")]
  97. public float horizontal = 0f;
  98. public float vertical = 0f;
  99. public float turn = 0f;
  100. public bool isGrounded = true;
  101. public bool isFalling = false;
  102.  
  103. void Update()
  104. {
  105. if (!isLocalPlayer) return;
  106.  
  107. horizontal = Input.GetAxis("Horizontal");
  108. vertical = Input.GetAxis("Vertical");
  109.  
  110. if (Input.GetKey(KeyCode.Q) && (turn > -maxTurnSpeed))
  111. turn -= turnSpeedAccel;
  112. else if (Input.GetKey(KeyCode.E) && (turn < maxTurnSpeed))
  113. turn += turnSpeedAccel;
  114. else if (turn > turnSpeedDecel)
  115. turn -= turnSpeedDecel;
  116. else if (turn < -turnSpeedDecel)
  117. turn += turnSpeedDecel;
  118. else
  119. turn = 0f;
  120.  
  121. if (!isFalling && Input.GetKey(KeyCode.Space) && (isGrounded || jumpSpeed < 1))
  122. jumpSpeed += jumpFactor;
  123. else if (isGrounded)
  124. isFalling = false;
  125. else
  126. {
  127. isFalling = true;
  128. jumpSpeed = 0;
  129. }
  130. }
  131. public bool Areweonmap = false;
  132. void FixedUpdate()
  133. {
  134. if (!isLocalPlayer || characterController == null) return;
  135. /*
  136. if (Areweonmap == true)
  137. {
  138. transform.Rotate(0f, turn * Time.fixedDeltaTime, 0f);
  139.  
  140. Vector3 direction = new Vector3(horizontal, jumpSpeed, vertical);
  141. direction = Vector3.ClampMagnitude(direction, 1f);
  142. direction = transform.TransformDirection(direction);
  143. direction *= moveSpeed;
  144.  
  145. if (jumpSpeed > 0)
  146. characterController.Move(direction * Time.fixedDeltaTime);
  147. else
  148. characterController.SimpleMove(direction);
  149.  
  150. isGrounded = characterController.isGrounded;
  151. }*/
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement