Advertisement
Guest User

Untitled

a guest
Feb 7th, 2016
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. using UnityEngine;
  2. using System.Collections.Generic;
  3. using UnityEngine.Networking;
  4.  
  5. public class serverController : NetworkBehaviour
  6. {
  7.  
  8. public GameObject serverCCPrefab;
  9. public CharacterController serverCC;
  10.  
  11. private clientController clientControllerScript;
  12.  
  13. //Units covered in 1 second
  14. public int walkSpeed = 2;
  15. public int runSpeed = 4;
  16. private int curSpeed;
  17.  
  18. private Vector3 serverResult;
  19.  
  20. void Start ()
  21. {
  22. if (isServer)
  23. {
  24. GameObject cloneCC = (GameObject)Instantiate(serverCCPrefab, Vector2.zero, transform.rotation);
  25. serverCC = cloneCC.GetComponent<CharacterController>();
  26. }
  27.  
  28. clientControllerScript = GetComponent<clientController>();
  29. curSpeed = walkSpeed;
  30. }
  31.  
  32.  
  33. //Changed it so that the server insantly syncs to client pakcets instead of doing it over 5 fixedupdates
  34. public void PacketSync (Vector4[] packet)
  35. {
  36. foreach (var item in packet)
  37. {
  38. Vector2 curReceivedInput = (new Vector2(item.x, item.y));
  39.  
  40. serverCC.Move(curReceivedInput * Time.fixedDeltaTime * curSpeed);
  41. }
  42.  
  43. serverResult = new Vector3(serverCC.gameObject.transform.position.x, serverCC.gameObject.transform.position.y, (int)packet[packet.Length - 1].w);
  44.  
  45. RpcResultFromServer(serverResult);
  46. }
  47.  
  48. [ClientRpc]
  49. void RpcResultFromServer (Vector3 result)
  50. {
  51. if (clientControllerScript)
  52. {
  53. clientControllerScript.ApplyResult(result);
  54. }
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement