Advertisement
djgaven588

MultiplayerPlayer

Apr 16th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class CharacterManager : MonoBehaviour {
  6.  
  7. public float Speed = 5f;
  8. public float JumpHeight = 2f;
  9. public float GroundDistance = 0.2f;
  10. public float sprintSpeed = 7.5f;
  11. public float mouseSensitivity = 5f;
  12. public float upDownRange = 85.0f;
  13. public float MouseSensitivity = 5.0f;
  14. float verticalRotation = 0f;
  15. public LayerMask Ground;
  16.  
  17. private Rigidbody _body;
  18. private Vector3 _inputs = Vector3.zero;
  19. private bool _isGrounded = true;
  20. private Transform _groundChecker;
  21. private Camera cam;
  22. private PhotonView mine;
  23. private bool isMine = false;
  24. Vector3 oldPosition;
  25. Vector3 networkPosition;
  26. Quaternion networkRotation;
  27.  
  28. [Header("Network Sync Settings")]
  29. public float moveSpeed = 25f;
  30. public float rotationSpeed = 300f;
  31.  
  32. void Awake()
  33. {
  34. mine = GetComponent<PhotonView>();
  35. _body = GetComponent<Rigidbody>();
  36. _groundChecker = transform.GetChild(0);
  37. if(!mine.isMine)
  38. {
  39. isMine = false;
  40. return;
  41. }
  42. isMine = true;
  43. cam = GetComponentInChildren<Camera>();
  44. cam.gameObject.SetActive(true);
  45. }
  46.  
  47. void Update()
  48. {
  49. if(isMine)
  50. {
  51. _isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore);
  52.  
  53. float rotLeftRight = Input.GetAxis("Mouse X") * mouseSensitivity;
  54.  
  55. transform.Rotate(0, rotLeftRight, 0);
  56.  
  57. verticalRotation -= Input.GetAxis("Mouse Y") * mouseSensitivity;
  58.  
  59. verticalRotation = Mathf.Clamp(verticalRotation, -upDownRange, upDownRange);
  60. cam.transform.localRotation = Quaternion.Euler(verticalRotation, 0, 0);
  61.  
  62. _inputs = Vector3.zero;
  63. _inputs.x = Input.GetAxis("Horizontal");
  64. _inputs.z = Input.GetAxis("Vertical");
  65.  
  66. forwardBackward = _inputs.x;
  67. leftRight = _inputs.z;
  68. jump = Input.GetButtonDown("Jump");
  69. sprinting = Input.GetKey(KeyCode.LeftShift);
  70.  
  71. if (Input.GetButtonDown("Jump") && _isGrounded)
  72. {
  73. _body.AddForce(Vector3.up * Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y), ForceMode.VelocityChange);
  74. }
  75. if (Input.GetKey(KeyCode.LeftShift) && _inputs.z > 0)
  76. {
  77. _inputs.z = _inputs.z * sprintSpeed;
  78. }
  79. }
  80. else
  81. {
  82. transform.position = Vector3.MoveTowards(transform.position, networkPosition, Time.fixedDeltaTime * moveSpeed);
  83. transform.rotation = Quaternion.RotateTowards(transform.rotation, networkRotation, Time.fixedDeltaTime * rotationSpeed);
  84. }
  85. }
  86. private int updatesPassed = 0;
  87. private float PlayerUpdate(bool jump, bool sprint, float forwards, float sideways)
  88. {
  89. _isGrounded = Physics.CheckSphere(_groundChecker.position, GroundDistance, Ground, QueryTriggerInteraction.Ignore);
  90. _inputs = Vector3.zero;
  91. _inputs.x = forwards;
  92. _inputs.z = sideways;
  93.  
  94. float f = 0;
  95. if (jump && _isGrounded)
  96. {
  97. jump = false;
  98. f = Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y);
  99. _body.AddForce(Vector3.up * Mathf.Sqrt(JumpHeight * -2f * Physics.gravity.y), ForceMode.VelocityChange);
  100. }
  101. if (sprint && _inputs.z > 0)
  102. {
  103. _inputs.z = _inputs.z * sprintSpeed;
  104. }
  105. return f;
  106. }
  107. private Vector3 SimulateUpdate(Vector3 inp, float force)
  108. {
  109. _body.MovePosition(_body.position + networkRotation * inp * Speed * (Time.fixedDeltaTime * updatesPassed));
  110. return transform.position + Vector3.up * force * (Time.fixedDeltaTime * updatesPassed);
  111. }
  112.  
  113. void FixedUpdate()
  114. {
  115. if(teleport == false)
  116. {
  117. _body.MovePosition(_body.position + transform.rotation * _inputs * Speed * Time.fixedDeltaTime);
  118. }
  119. else
  120. {
  121. teleport = false;
  122. _body.position = networkPosition;
  123. _body.rotation = networkRotation;
  124. }
  125. updatesPassed++;
  126. }
  127.  
  128. public float forwardBackward = 0;
  129. public float leftRight = 0;
  130. public bool jump = false;
  131. public bool sprinting = false;
  132. public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  133. {
  134. if(stream.isWriting)
  135. {
  136. stream.SendNext(forwardBackward);
  137. stream.SendNext(leftRight);
  138. stream.SendNext(jump);
  139. stream.SendNext(sprinting);
  140. stream.SendNext(transform.position);
  141. stream.SendNext(transform.rotation);
  142. Vector3 movement = transform.position - oldPosition;
  143. stream.SendNext(movement);
  144. oldPosition = transform.position;
  145. }
  146. else
  147. {
  148. float forwardBackward = (float)stream.ReceiveNext();
  149. float leftRight = (float)stream.ReceiveNext();
  150. bool jump = (bool)stream.ReceiveNext();
  151. bool sprinting = (bool)stream.ReceiveNext();
  152. networkPosition = (Vector3)stream.ReceiveNext();
  153. networkRotation = (Quaternion)stream.ReceiveNext();
  154. Vector3 dirM = (Vector3)stream.ReceiveNext();
  155. if(PhotonNetwork.isMasterClient)
  156. {
  157. float force = PlayerUpdate(jump, sprinting, forwardBackward, leftRight);
  158. float lagC = Mathf.Abs((float) (PhotonNetwork.time - info.timestamp));
  159. networkPosition += (dirM * lagC);
  160.  
  161. Vector3 pos = transform.position;
  162. SimulateUpdate(new Vector3(forwardBackward, 0, leftRight), force);
  163. updatesPassed = 0;
  164. float dist = Vector3.Distance(networkPosition, transform.position);
  165. transform.position = pos;
  166. if(dist > 0.25f)
  167. {
  168. GetComponent<PhotonView>().RPC("SetPosition", PhotonTargets.All, transform.position, transform.rotation);
  169. }
  170. }
  171. else
  172. {
  173. float lag = Mathf.Abs((float) (PhotonNetwork.time - info.timestamp));
  174. networkPosition += (dirM * lag);
  175. }
  176. }
  177. }
  178. private bool teleport = false;
  179. [PunRPC]
  180. public void SetPosition(Vector3 position, Quaternion rotation)
  181. {
  182. transform.position = position;
  183. transform.rotation = rotation;
  184. networkPosition = position;
  185. networkRotation = rotation;
  186. teleport = true;
  187. }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement