Advertisement
Guest User

AnySync simple example

a guest
Jul 19th, 2018
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.74 KB | None | 0 0
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3.  
  4. namespace AnySync.Examples
  5. {
  6.     /// <summary>
  7.     /// Minimal example using the easiest possible techniques.
  8.     /// Some of the best practices are ignored in favor of shorter code.
  9.     /// </summary>
  10.     public class UNetPlayerSync2D : NetworkBehaviour
  11.     {
  12.         private const float MinimumSendInterval = 0.05f; // up to 20 messages per second
  13.  
  14.         private const float MovementSpeed = 5f;
  15.         private const float JumpVelocity = 8f;
  16.  
  17.  
  18.         private float _timeSinceLastSync;
  19.         private Vector3 _lastSentPosition;
  20.         private bool _idle;
  21.         private void Update()
  22.         {
  23.             if (isLocalPlayer)
  24.             {
  25.                 var rigidbody2DComponent = GetComponent<Rigidbody2D>();
  26.  
  27.                 // get original velocity
  28.                 var rigidbody2DVelocity = rigidbody2DComponent.velocity;
  29.  
  30.                 // modify velocity according to input
  31.                 rigidbody2DVelocity.x = Input.GetAxisRaw("Horizontal") * MovementSpeed;
  32.                 if (Input.GetButtonDown("Jump"))
  33.                     rigidbody2DVelocity.y = JumpVelocity;
  34.  
  35.                 // apply velocity change
  36.                 rigidbody2DComponent.velocity = rigidbody2DVelocity;
  37.  
  38.                 // in the nutshell, this commented out code does the same thing as lines 47-65
  39.                 // but to stop player from sending messages while idle, some extra code is required
  40.                 // you can decide which one to use for your game
  41.                 //if (_timeSinceLastSync >= MinimumSendInterval)
  42.                 //{
  43.                 //    CmdSync(_timeSinceLastSync, rigidbody2DComponent.position);
  44.                 //    _timeSinceLastSync = 0f;
  45.                 //}
  46.  
  47.                 // send sync messages
  48.                 _timeSinceLastSync += Time.deltaTime;
  49.                 if (_timeSinceLastSync >= MinimumSendInterval)
  50.                 {
  51.                     if (_lastSentPosition != transform.position)
  52.                         _idle = false;
  53.  
  54.                     if (!_idle)
  55.                     {
  56.                         CmdSync(_timeSinceLastSync, transform.position);
  57.                         // go idle only after sending a keyframe with the same position to prevent drifting while idle
  58.                         if (_lastSentPosition == transform.position)
  59.                             _idle = true;
  60.  
  61.                         _lastSentPosition = transform.position;
  62.                         _timeSinceLastSync = 0f;
  63.                     }
  64.                 }
  65.             }
  66.             else
  67.             {
  68.                 var syncBufferComponent = GetComponent<SyncBuffer>();
  69.                 // avoid warnings if syncBuffer is empty
  70.                 if (syncBufferComponent.HasKeyframes)
  71.                 {
  72.                     // update playback and apply new position
  73.                     syncBufferComponent.UpdatePlayback(Time.deltaTime);
  74.                     transform.position = syncBufferComponent.Position;
  75.                 }
  76.             }
  77.         }
  78.  
  79.         [Command]
  80.         private void CmdSync(float interpolationTime, Vector2 position)
  81.         {
  82.             // add keyframe to buffer on server
  83.             GetComponent<SyncBuffer>().AddKeyframe(interpolationTime, position);
  84.             // send it to other clients
  85.             RpcSync(interpolationTime, position);
  86.         }
  87.  
  88.         [ClientRpc]
  89.         private void RpcSync(float interpolationTime, Vector2 position)
  90.         {
  91.             // prevent receiving keyframes on owner client and host
  92.             if (isLocalPlayer || isServer)
  93.                 return;
  94.  
  95.             // add keyframe to buffer on clients
  96.             GetComponent<SyncBuffer>().AddKeyframe(interpolationTime, position);
  97.         }
  98.     }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement