Advertisement
Guest User

Untitled

a guest
Oct 30th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.68 KB | None | 0 0
  1. /////NETWORKMOVEMENT SCRIPT/////
  2.  
  3. using UnityEngine;
  4. using System.Collections;
  5. using UnityEngine.Networking;
  6. using System.Collections.Generic;
  7.  
  8. [NetworkSettings(channel = 1, sendInterval = 0.05f)]
  9. public class NetworkMovement : NetworkBehaviour
  10. {
  11. public int moveDelay = 15;
  12. public bool server = false;
  13.  
  14. [System.NonSerialized]
  15. public int currentDelay = 0;
  16.  
  17. public struct Inputs
  18. {
  19. public bool N;
  20. public bool S;
  21. public bool E;
  22. public bool W;
  23. }
  24.  
  25. //Used by server to collect results of Move() before sending to client
  26. public struct Results
  27. {
  28. public Vector3 position;
  29. }
  30.  
  31. //Synced from server to all clients
  32. // [SyncVar(hook = "RecieveResults")]
  33. // private Results syncResults;
  34.  
  35. private Inputs inputStruct;
  36. // private List<Inputs> inputsList = new List<Inputs>();
  37.  
  38. // Use this for initialization
  39. void Start()
  40. {
  41. Debug.Log("NetworkMovement exists!");
  42. }
  43.  
  44. void Update()
  45. {
  46. if (isLocalPlayer && !server)
  47. {
  48. //Getting clients inputs
  49. GetInputs(ref inputStruct);
  50. }
  51. }
  52.  
  53. void FixedUpdate()
  54. {
  55. if (hasAuthority && !server)
  56. {
  57. Debug.Log("hasAuthority returned true in Update() of NetworkMovement, moving...");
  58. //Struct need to be fully new to sync, hence the temporary results struct
  59. Results tempResults;
  60. tempResults.position = Move(inputStruct);
  61. //syncResults = tempResults;
  62. }
  63. }
  64.  
  65. public virtual void GetInputs(ref Inputs inputs)
  66. {
  67. inputs.N = Input.GetKey(KeyCode.W);
  68. inputs.S = Input.GetKey(KeyCode.S);
  69. inputs.E = Input.GetKey(KeyCode.D);
  70. inputs.W = Input.GetKey(KeyCode.A);
  71.  
  72. Debug.Log("Inputs: " + inputs.N + inputs.S + inputs.E + inputs.W);
  73. }
  74.  
  75. public virtual Vector3 Move(Inputs inputs)
  76. {
  77. Debug.Log("Move has been called.");
  78. if (currentDelay < 1)
  79. {
  80. Vector3 movement = Vector3.zero;
  81.  
  82. if (inputs.N && !inputs.S)
  83. {
  84. movement = transform.up;
  85. }
  86. else if (!inputs.N && inputs.S)
  87. {
  88. movement = -(transform.up);
  89. }
  90. else if (inputs.E && !inputs.W)
  91. {
  92. movement = transform.right;
  93. }
  94. else if (!inputs.E && inputs.W)
  95. {
  96. movement = -(transform.right);
  97. }
  98.  
  99. transform.Translate(movement);
  100. }
  101. else
  102. {
  103. currentDelay--;
  104. }
  105.  
  106. return transform.position;
  107. }
  108. }
  109.  
  110.  
  111.  
  112.  
  113. ///// PLAYERMOVEMENT SCRIPT (NETWORKPAWN RENAMED) /////
  114.  
  115. using UnityEngine;
  116. using System.Collections;
  117.  
  118. public class PlayerMovement : NetworkMovement {
  119. public Transform playerTransform;
  120.  
  121. public override Vector3 Move(Inputs inputs)
  122. {
  123. if (currentDelay < 1)
  124. {
  125. Vector3 movement = Vector3.zero;
  126.  
  127. if (inputs.N && !inputs.S)
  128. {
  129. movement = transform.up;
  130. }
  131. else if (!inputs.N && inputs.S)
  132. {
  133. movement = -(transform.up);
  134. }
  135. else if (inputs.E && !inputs.W)
  136. {
  137. movement = transform.right;
  138. }
  139. else if (!inputs.E && inputs.W)
  140. {
  141. movement = -(transform.right);
  142. }
  143.  
  144. playerTransform.Translate(movement);
  145. }
  146. else
  147. {
  148. currentDelay--;
  149. }
  150.  
  151. return playerTransform.position;
  152. }
  153. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement