Guest User

AnySync PUN2 code example.

a guest
Jun 30th, 2019
129
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using Photon.Pun;
  2. using UnityEngine;
  3.  
  4. namespace AnySync.Examples
  5. {
  6.     /// <summary>
  7.     /// Advanced example ported to Photon Unity Networking 2.
  8.     /// Built on top of serialization instead of using RPCs.
  9.     /// </summary>
  10.     public class PUN2PlayerSync : MonoBehaviourPunCallbacks, IPunObservable
  11.     {
  12.         private const float CharacterSpeed = 3f;
  13.         private const float CharacterRotationSensitivity = 3f;
  14.  
  15.         private MotionGenerator _motionGenerator;
  16.  
  17.         private float _angularVelocity;
  18.         private bool _teleportOnNextSync;
  19.         private double _lastReceivedKeyframeTime = double.NegativeInfinity;
  20.  
  21.         private CharacterController _characterController;
  22.         private void Awake()
  23.         {
  24.             _characterController = GetComponent<CharacterController>();
  25.             // Adding fixedDeltaTime to interpolationLatency because Photon updates in a fixed timestep. It's a packet jitter safety margin.
  26.             var interpolationLatency = 1f / PhotonNetwork.SerializationRate + Time.fixedDeltaTime;
  27.             _motionGenerator = new MotionGenerator(interpolationLatency);
  28.         }
  29.  
  30.         private void Update()
  31.         {
  32.             if (photonView.IsMine)
  33.             {
  34.                 // Teleportation.
  35.                 if (Input.GetKeyDown(KeyCode.Space))
  36.                 {
  37.                     var newPosition = new Vector3(Random.Range(-6f, 6f), 2f, Random.Range(-6f, 6f));
  38.                     // Disabling characterController first so it doesn't override the teleport.
  39.                     _characterController.enabled = false;
  40.                     transform.position = newPosition;
  41.                     _characterController.enabled = true;
  42.  
  43.                     // In order to teleport, we have to send an additional keyframe with 0 interpolation time.
  44.                     // Since we can't (or don't want to) manually invoke the serialization event, we have to work around it.
  45.                     _teleportOnNextSync = true;
  46.                     return;
  47.                 }
  48.  
  49.                 // Local movement.
  50.                 var movementInput = Vector2.ClampMagnitude(new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")), 1f);
  51.                 _characterController.SimpleMove(new Vector3(-movementInput.x, 0f, -movementInput.y) * CharacterSpeed);
  52.                 _angularVelocity = Input.GetAxisRaw("Mouse X") * CharacterRotationSensitivity;
  53.                 transform.Rotate(0f, _angularVelocity, 0f);
  54.             }
  55.             else
  56.             {
  57.                 // Proxy movement.
  58.                 if (_motionGenerator.HasKeyframes)
  59.                 {
  60.                     _motionGenerator.UpdatePlayback(Time.deltaTime);
  61.                     transform.position = _motionGenerator.Position;
  62.                     transform.rotation = _motionGenerator.Rotation;
  63.                 }
  64.             }
  65.         }
  66.  
  67.         public void OnPhotonSerializeView(PhotonStream stream, PhotonMessageInfo info)
  68.         {
  69.             if (stream.IsWriting)
  70.             {
  71.                 // Sending keyframe data.
  72.                 stream.SendNext(transform.position);
  73.                 stream.SendNext(_characterController.velocity);
  74.                 stream.SendNext(transform.rotation.eulerAngles.y);
  75.                 stream.SendNext(_angularVelocity);
  76.                 stream.SendNext(_teleportOnNextSync);
  77.  
  78.                 // Reset the teleport trigger.
  79.                 _teleportOnNextSync = false;
  80.             }
  81.             else
  82.             {
  83.                 // Receiving keyframe data.
  84.                 var position = (Vector3)stream.ReceiveNext();
  85.                 var velocity = (Vector3)stream.ReceiveNext();
  86.                 var rotation = (float)stream.ReceiveNext();
  87.                 var angularVelocity = (float)stream.ReceiveNext();
  88.                 var isTeleport = (bool)stream.ReceiveNext();
  89.  
  90.                 // Get the message timestamp and use it as a "world time" for this keyframe.
  91.                 var keyframeTime = info.SentServerTime;
  92.  
  93.                 // Make the first keyframe instant to avoid issues.
  94.                 if (_lastReceivedKeyframeTime < 0)
  95.                     _lastReceivedKeyframeTime = keyframeTime;
  96.  
  97.                 // Get time difference from last keyframe and feed it as interpolation time.
  98.                 var interpolationTime = (float)(keyframeTime - _lastReceivedKeyframeTime);
  99.                 _lastReceivedKeyframeTime = keyframeTime;
  100.  
  101.                 if (isTeleport)
  102.                 {
  103.                     // To keep timing consistent (to avoid time drift), duplicate last keyframe before teleporting.
  104.                     if (_motionGenerator.HasKeyframes)
  105.                         _motionGenerator.AddKeyframe(interpolationTime, _motionGenerator.LastReceivedKeyframe.Position, _motionGenerator.LastReceivedKeyframe.Rotation);
  106.                    
  107.                     // And send a 0 interpolation time keyframe to trigger the teleport to new position.
  108.                     _motionGenerator.AddKeyframe(0f, position, velocity, Quaternion.Euler(0f, rotation, 0f), new Vector3(0f, angularVelocity, 0f));
  109.                 }
  110.                 else
  111.                 {
  112.                     _motionGenerator.AddKeyframe(interpolationTime, position, velocity, Quaternion.Euler(0f, rotation, 0f), new Vector3(0f, angularVelocity, 0f));
  113.                 }
  114.             }
  115.         }
  116.     }
  117. }
RAW Paste Data