Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using UnityEngine;
- using System.Collections.Generic;
- using UnityEngine.Networking;
- public class serverController : NetworkBehaviour
- {
- public GameObject serverCCPrefab;
- public CharacterController serverCC;
- private clientController clientControllerScript;
- //Units covered in 1 second
- public int walkSpeed = 2;
- public int runSpeed = 4;
- private int curSpeed;
- private Vector3 serverResult;
- void Start ()
- {
- if (isServer)
- {
- GameObject cloneCC = (GameObject)Instantiate(serverCCPrefab, Vector2.zero, transform.rotation);
- serverCC = cloneCC.GetComponent<CharacterController>();
- }
- clientControllerScript = GetComponent<clientController>();
- curSpeed = walkSpeed;
- }
- //Changed it so that the server insantly syncs to client pakcets instead of doing it over 5 fixedupdates
- public void PacketSync (Vector4[] packet)
- {
- foreach (var item in packet)
- {
- Vector2 curReceivedInput = (new Vector2(item.x, item.y));
- serverCC.Move(curReceivedInput * Time.fixedDeltaTime * curSpeed);
- }
- serverResult = new Vector3(serverCC.gameObject.transform.position.x, serverCC.gameObject.transform.position.y, (int)packet[packet.Length - 1].w);
- RpcResultFromServer(serverResult);
- }
- [ClientRpc]
- void RpcResultFromServer (Vector3 result)
- {
- if (clientControllerScript)
- {
- clientControllerScript.ApplyResult(result);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement