Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- -------------------------------------------------------
- Developer: Alexander - twitter.com/wobes_1
- Date: 19/11/2017 00:04
- -------------------------------------------------------
- */
- using BEING.Networking.Management;
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.Networking;
- namespace BEING.Networking
- {
- public class NewNetworkController : NetworkBehaviour
- {
- public struct ClientPacket
- {
- public float Horizontal;
- public float Vertical;
- public Vector3 Position;
- }
- private CharacterController characterController;
- public float MoveSpeed = 2.0f;
- private Vector3 moveDirection;
- private float horizontal;
- private float vertical;
- private Queue<ClientPacket> packetQueue = new Queue<ClientPacket>();
- private void Start()
- {
- characterController = this.GetComponent<CharacterController>();
- }
- private void Update()
- {
- if (isLocalPlayer)
- {
- horizontal = Input.GetAxisRaw("Horizontal");
- vertical = Input.GetAxisRaw("Vertical");
- }
- }
- private void FixedUpdate()
- {
- if (isLocalPlayer)
- {
- ClientPacket clientPacket = new ClientPacket
- {
- Horizontal = horizontal,
- Vertical = vertical
- };
- Simulate(clientPacket.Horizontal, clientPacket.Vertical);
- clientPacket.Position = transform.position;
- CmdSendInput(clientPacket);
- }
- if (NetworkServer.active)
- {
- if (packetQueue.Count == 0)
- return;
- var inputPacket = packetQueue.Dequeue();
- Simulate(inputPacket.Horizontal, inputPacket.Vertical);
- Debug.Log(string.Format("Server posResult: {0} | Client posResult: {1}", transform.position, inputPacket.Position));
- }
- }
- [Command(channel = NetworkChannels.DefaultUnreliable)]
- public void CmdSendInput(ClientPacket clientPacket)
- {
- packetQueue.Enqueue(clientPacket);
- }
- public void Simulate(float horizontalAxis, float verticalAxis)
- {
- moveDirection = transform.TransformDirection(new Vector3(horizontalAxis, 0, verticalAxis));
- moveDirection *= MoveSpeed;
- characterController.Move(moveDirection * Time.fixedDeltaTime);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement