Guest User

AnySync Basic code example.

a guest
May 16th, 2019
80
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using UnityEngine;
  2. using UnityEngine.Networking;
  3.  
  4. namespace AnySync.Examples
  5. {
  6.     /// <summary>
  7.     /// Basic example using the easiest possible techniques.
  8.     /// Some best practices are ignored in favor of shorter code.
  9.     /// </summary>
  10.     public class UNetRigidbody2DSync : NetworkBehaviour
  11.     {
  12.         // Up to 10 messages per second.
  13.         private const float MinimumSendInterval = 0.1f;
  14.         private const float CharacterSpeed = 3f;
  15.         private const float JumpVelocity = 7f;
  16.  
  17.         private readonly MotionGenerator _motionGenerator = new MotionGenerator();
  18.  
  19.         private float _timeSinceLastSync;
  20.         private void Update()
  21.         {
  22.             if (hasAuthority)
  23.             {
  24.                 // Local movement.
  25.  
  26.                 var rigidbody2DComponent = GetComponent<Rigidbody2D>();
  27.  
  28.                 // Get original velocity.
  29.                 var rigidbody2DVelocity = rigidbody2DComponent.velocity;
  30.  
  31.                 // Modify velocity according to input.
  32.                 rigidbody2DVelocity.x = Input.GetAxisRaw("Horizontal") * CharacterSpeed;
  33.                 if (Input.GetButtonDown("Jump"))
  34.                     rigidbody2DVelocity.y = JumpVelocity;
  35.  
  36.                 // Apply velocity change.
  37.                 rigidbody2DComponent.velocity = rigidbody2DVelocity;
  38.  
  39.                 // Sync position.
  40.                 _timeSinceLastSync += Time.deltaTime;
  41.                 if (_timeSinceLastSync >= MinimumSendInterval)
  42.                 {
  43.                     CmdSync(_timeSinceLastSync, transform.position);
  44.                     _timeSinceLastSync = 0f;
  45.                 }
  46.             }
  47.             else
  48.             {
  49.                 // Proxy movement.
  50.  
  51.                 // Avoid warnings if syncBuffer is empty.
  52.                 if (_motionGenerator.HasKeyframes)
  53.                 {
  54.                     // Update playback and apply new position.
  55.                     _motionGenerator.UpdatePlayback(Time.deltaTime);
  56.                     transform.position = _motionGenerator.Position;
  57.                 }
  58.             }
  59.         }
  60.  
  61.         [Command]
  62.         private void CmdSync(float interpolationTime, Vector2 position)
  63.         {
  64.             // Add keyframe to buffer on server.
  65.             _motionGenerator.AddKeyframe(interpolationTime, position);
  66.             // Send it to other clients.
  67.             RpcSync(interpolationTime, position);
  68.         }
  69.  
  70.         [ClientRpc]
  71.         private void RpcSync(float interpolationTime, Vector2 position)
  72.         {
  73.             // Prevent receiving keyframes on owner client and host.
  74.             if (isLocalPlayer || isServer)
  75.                 return;
  76.  
  77.             // Add keyframe to buffer on clients.
  78.             _motionGenerator.AddKeyframe(interpolationTime, position);
  79.         }
  80.     }
  81. }
RAW Paste Data