Advertisement
Guest User

Untitled

a guest
Nov 19th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. /*
  2. -------------------------------------------------------
  3.  Developer:  Alexander - twitter.com/wobes_1
  4.  Date:       19/11/2017 00:04
  5. -------------------------------------------------------
  6. */
  7.  
  8. using BEING.Networking.Management;
  9. using System.Collections;
  10. using System.Collections.Generic;
  11. using UnityEngine;
  12. using UnityEngine.Networking;
  13.  
  14. namespace BEING.Networking
  15. {
  16.     public class NewNetworkController : NetworkBehaviour
  17.     {
  18.         public struct ClientPacket
  19.         {
  20.             public float Horizontal;
  21.             public float Vertical;
  22.             public Vector3 Position;
  23.         }
  24.  
  25.         private CharacterController characterController;
  26.  
  27.         public float MoveSpeed = 2.0f;
  28.         private Vector3 moveDirection;
  29.         private float horizontal;
  30.         private float vertical;
  31.  
  32.         private Queue<ClientPacket> packetQueue = new Queue<ClientPacket>();
  33.  
  34.         private void Start()
  35.         {
  36.             characterController = this.GetComponent<CharacterController>();
  37.         }
  38.  
  39.         private void Update()
  40.         {
  41.             if (isLocalPlayer)
  42.             {
  43.                 horizontal = Input.GetAxisRaw("Horizontal");
  44.                 vertical = Input.GetAxisRaw("Vertical");
  45.             }
  46.         }
  47.  
  48.         private void FixedUpdate()
  49.         {
  50.             if (isLocalPlayer)
  51.             {
  52.                 ClientPacket clientPacket = new ClientPacket
  53.                 {
  54.                     Horizontal = horizontal,
  55.                     Vertical = vertical
  56.                 };
  57.  
  58.                 Simulate(clientPacket.Horizontal, clientPacket.Vertical);
  59.                 clientPacket.Position = transform.position;
  60.  
  61.                 CmdSendInput(clientPacket);
  62.             }
  63.  
  64.             if (NetworkServer.active)
  65.             {
  66.                 if (packetQueue.Count == 0)
  67.                     return;
  68.  
  69.                 var inputPacket = packetQueue.Dequeue();
  70.  
  71.                 Simulate(inputPacket.Horizontal, inputPacket.Vertical);
  72.  
  73.                 Debug.Log(string.Format("Server posResult: {0} | Client posResult: {1}", transform.position, inputPacket.Position));
  74.             }
  75.         }
  76.  
  77.         [Command(channel = NetworkChannels.DefaultUnreliable)]
  78.         public void CmdSendInput(ClientPacket clientPacket)
  79.         {
  80.             packetQueue.Enqueue(clientPacket);
  81.         }
  82.  
  83.         public void Simulate(float horizontalAxis, float verticalAxis)
  84.         {
  85.             moveDirection = transform.TransformDirection(new Vector3(horizontalAxis, 0, verticalAxis));
  86.             moveDirection *= MoveSpeed;
  87.  
  88.             characterController.Move(moveDirection * Time.fixedDeltaTime);
  89.         }
  90.     }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement