Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.13 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Mirror;
  5. using System.Timers;
  6.  
  7. public class PlayerControler : NetworkBehaviour
  8. {
  9. [Header("Player Move Settings")]
  10. public float Speed = 5f;
  11. public float MaximumVelocity = 2f;
  12. public float JumpForce = 3f;
  13.  
  14. private bool isJump;
  15.  
  16. private Rigidbody rig;
  17. private GroundCheck GroundCheck;
  18.  
  19. private Vector3 velocity;
  20. private Vector2 input;
  21. private Vector3 serverPos;
  22. private SteamNetworkManager networkManager;
  23.  
  24. private void OnEnable()
  25. {
  26. GroundCheck = GameObject.FindGameObjectWithTag("Player").GetComponent<GroundCheck>();
  27. rig = GetComponent<Rigidbody>();
  28. }
  29.  
  30. private void Awake()
  31. {
  32. networkManager = SteamNetworkManager.instance;
  33. }
  34.  
  35. private void FixedUpdate()
  36. {
  37. print("Client: " + netId);
  38. if(isLocalPlayer && isServer)
  39. {
  40. GetInput();
  41. }
  42.  
  43. if (isServer)
  44. {
  45. MovePlayer();
  46. }
  47.  
  48. CmdJump(false);
  49. }
  50. private void MovePlayer()
  51. {
  52. if (isJump)
  53. {
  54. rig.AddForce(new Vector3(0, JumpForce * 80, 0), ForceMode.Acceleration);
  55. }
  56.  
  57. float speedToDelta = Speed * Time.fixedDeltaTime * 2;
  58.  
  59. float yVelocity = rig.velocity.y;
  60.  
  61. rig.velocity = new Vector3(speedToDelta * input.x, 0, speedToDelta * input.y).normalized * MaximumVelocity;
  62.  
  63. Vector3 v = rig.velocity;
  64. v.y = yVelocity;
  65.  
  66. rig.velocity = v;
  67. }
  68.  
  69. [Command]
  70. private void CmdReadInput(float x,float z)
  71. {
  72. input.x = x;
  73. input.y = z;
  74. }
  75.  
  76. [Command]
  77. private void CmdJump(bool jump)
  78. {
  79. isJump = jump;
  80. }
  81.  
  82. private void SyncClientPosition(object sender, ElapsedEventArgs e)
  83. {
  84. UnityMainThreadDispatcher.Instance().Enqueue(() => CmdSyncClientPosition(transform.position));
  85. }
  86.  
  87. private void SyncClientRotation(object sender, ElapsedEventArgs e)
  88. {
  89. UnityMainThreadDispatcher.Instance().Enqueue(() => CmdSyncClientRotation(transform.eulerAngles));
  90. }
  91.  
  92. [Command]
  93. private void CmdSyncClientPosition(Vector3 serverPosition)
  94. {
  95. TargetSyncToServerPosition(serverPosition);
  96. }
  97.  
  98. [TargetRpc]
  99. private void TargetSyncToServerPosition(Vector3 serverPosition)
  100. {
  101. serverPos = serverPosition;
  102. }
  103.  
  104. [Command]
  105. private void CmdSyncClientRotation(Vector3 clientRotation)
  106. {
  107. transform.eulerAngles = clientRotation;
  108. }
  109.  
  110.  
  111. private void GetInput()
  112. {
  113. if (Input.GetButtonDown("Jump") && GroundCheck.IsOnGround == true)
  114. {
  115. CmdJump(true);
  116. }
  117. else
  118. {
  119. CmdJump(false);
  120. }
  121.  
  122.  
  123. float horizontal = Input.GetAxis("Horizontal");
  124. float vertical = Input.GetAxis("Vertical");
  125.  
  126. input = new Vector2(horizontal, vertical);
  127.  
  128. if (input.sqrMagnitude > 1)
  129. {
  130. input.Normalize();
  131. }
  132.  
  133. CmdReadInput(input.x,input.y);
  134. MovePlayer();
  135. }
  136.  
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement