Advertisement
Guest User

AnySync example for Photon

a guest
Jul 19th, 2018
188
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.73 KB | None | 0 0
  1. using Photon;
  2. using UnityEngine;
  3.  
  4. namespace AnySync.Examples
  5. {
  6.     public class PhotonPlayerSync : PunBehaviour, IPunObservable
  7.     {
  8.         /// <summary>
  9.         /// Used to indicate sync message in the stream.
  10.         /// </summary>
  11.         private const int StreamMessageHeader = 0xCABAD6E;
  12.  
  13.         private const float MovementAcceleration = 13f;
  14.  
  15.         private bool _teleportOnNextSync;
  16.         private Vector2 _input;
  17.  
  18.         private Rigidbody _rigidbody;
  19.         private SyncBuffer _syncBuffer;
  20.         private Transform _transform;
  21.         private void Awake()
  22.         {
  23.             _rigidbody = GetComponent<Rigidbody>();
  24.             _syncBuffer = GetComponent<SyncBuffer>();
  25.             _transform = transform;
  26.         }
  27.  
  28.         private void Update()
  29.         {
  30.             if (!photonView.isMine)
  31.             {
  32.                 if (_syncBuffer.HasKeyframes)
  33.                 {
  34.                     _syncBuffer.UpdatePlayback(Time.deltaTime);
  35.                     _transform.position = _syncBuffer.Position;
  36.                     _transform.rotation = _syncBuffer.Rotation;
  37.                 }
  38.             }
  39.             else
  40.             {
  41.                 // movement
  42.                 _input = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical"));
  43.  
  44.                 // teleportation
  45.                 if (Input.GetKeyDown(KeyCode.Space))
  46.                 {
  47.                     var newPosition = new Vector3(Random.Range(-6f, 6f), 2f, Random.Range(-6f, 6f));
  48.                     _rigidbody.position = newPosition;
  49.                     _rigidbody.rotation = Quaternion.identity;
  50.                     _rigidbody.velocity = Vector3.zero;
  51.                     _rigidbody.angularVelocity = Vector3.zero;
  52.                     _teleportOnNextSync = true;
  53.                 }
  54.             }
  55.         }
  56.  
  57.         private float _timeSinceLastSync;
  58.         private void FixedUpdate()
  59.         {
  60.             if (photonView.isMine)
  61.             {
  62.                 // control the rigidbody locally
  63.                 _rigidbody.AddForce(new Vector3(-_input.x, 0f, -_input.y) * MovementAcceleration * Time.deltaTime, ForceMode.VelocityChange);
  64.                 _timeSinceLastSync += Time.deltaTime;
  65.             }
  66.         }
  67.  
  68.         private bool _forceSync;
  69.         public override void OnPhotonPlayerConnected(PhotonPlayer newPlayer)
  70.         {
  71.             base.OnPhotonPlayerConnected(newPlayer);
  72.            
  73.             _forceSync = true;
  74.         }
  75.  
  76.         private Vector3 _lastSentVelocity;
  77.         public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  78.         {
  79.             if (stream.isWriting)
  80.             {
  81.                 if (!_forceSync && _rigidbody.velocity == _lastSentVelocity)
  82.                     return;
  83.  
  84.                 _forceSync = false;
  85.  
  86.                 stream.SendNext(StreamMessageHeader);
  87.                 stream.SendNext(_timeSinceLastSync);
  88.                 stream.SendNext(_rigidbody.position);
  89.                 stream.SendNext(_rigidbody.rotation);
  90.                 stream.SendNext(_rigidbody.velocity);
  91.                 stream.SendNext(_teleportOnNextSync);
  92.  
  93.                 _lastSentVelocity = _rigidbody.velocity;
  94.                 _teleportOnNextSync = false;
  95.                 _timeSinceLastSync = 0f;
  96.             }
  97.             else
  98.             {
  99.                 // avoid stealing stream data from other components if sync message was skipped
  100.                 var header = stream.PeekNext();
  101.                 if (header is int)
  102.                 {
  103.                     if ((int)header == StreamMessageHeader)
  104.                         stream.ReceiveNext(); // skip the header
  105.                     else
  106.                         return;
  107.                 }
  108.                 else
  109.                 {
  110.                     return;
  111.                 }
  112.  
  113.                 var interpolationTime = (float)stream.ReceiveNext();
  114.                 var position = (Vector3)stream.ReceiveNext();
  115.                 var rotation = (Quaternion)stream.ReceiveNext();
  116.                 var velocity = (Vector3)stream.ReceiveNext();
  117.                 var isTeleport = (bool)stream.ReceiveNext();
  118.  
  119.                 if (isTeleport)
  120.                 {
  121.                     if (_syncBuffer.HasKeyframes)
  122.                         _syncBuffer.AddKeyframe(interpolationTime, _syncBuffer.LastReceivedKeyframe.Position, _syncBuffer.LastReceivedKeyframe.Rotation, _syncBuffer.LastReceivedKeyframe.Velocity, _syncBuffer.LastReceivedKeyframe.AngularVelocity);
  123.  
  124.                     _syncBuffer.AddKeyframe(0f, position, rotation, velocity);
  125.                 }
  126.                 else
  127.                 {
  128.                     _syncBuffer.AddKeyframe(interpolationTime, position, rotation, velocity);
  129.                 }
  130.             }
  131.         }
  132.     }
  133. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement