Advertisement
Guest User

Untitled

a guest
Jun 7th, 2024
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.77 KB | Source Code | 0 0
  1. // DATA STRUCTURES
  2. public struct ReplicateData : IReplicateData
  3. {
  4.     public bool Jump;
  5.     public float Horizontal;
  6.     public float Vertical;
  7.     public float LookRotationY;
  8.  
  9.  
  10.     public ReplicateData(bool jump, float horizontal, float vertical, float lookrotationy) : this()
  11.     {
  12.         Jump = jump;
  13.         Horizontal = horizontal;
  14.         Vertical = vertical;
  15.         LookRotationY = lookrotationy;
  16.         _tick = 0;
  17.     }
  18.  
  19.     private uint _tick;
  20.     public void Dispose() { }
  21.     public uint GetTick() => _tick;
  22.     public void SetTick(uint value) => _tick = value;
  23. }
  24.  
  25. public struct ReconcileData : IReconcileData
  26. {
  27.  
  28.     public PredictionRigidbody PredictionRigidbody;
  29.  
  30.     public ReconcileData(PredictionRigidbody pr) : this()
  31.     {
  32.         PredictionRigidbody = pr;
  33.     }
  34.  
  35.     private uint _tick;
  36.     public void Dispose() { }
  37.     public uint GetTick() => _tick;
  38.     public void SetTick(uint value) => _tick = value;
  39. }
  40.  
  41. private ReplicateData CreateReplicateData()
  42. {
  43.     if (!base.IsOwner)
  44.     {
  45.         return default;
  46.     }
  47.  
  48.     float horizontal = Input.GetAxisRaw("Horizontal");
  49.     float vertical = Input.GetAxisRaw("Vertical");
  50.     float lookRotationy = lookRotationY;
  51.    
  52.     ReplicateData md = new ReplicateData(_jump, horizontal, vertical, lookRotationy);
  53.  
  54.     _jump = false;
  55.     return md;
  56. }
  57.  
  58. [Replicate]
  59. void RunInputs(ReplicateData data, ReplicateState state = ReplicateState.Invalid, Channel channel = Channel.Unreliable)
  60. {
  61.     if (!base.IsOwner)
  62.     {
  63.         return;
  64.     }
  65.  
  66.     Vector3 currentVelocity = PredictionRigidbody.Rigidbody.velocity;
  67.  
  68.     Vector3 targetVelocity = new Vector3(data.Horizontal, 0, data.Vertical) * _moveRate;
  69.     targetVelocity = transform.TransformDirection(targetVelocity);
  70.     Vector3 velocityChange = targetVelocity - currentVelocity;
  71.     velocityChange = new Vector3(velocityChange.x, 0, velocityChange.z);
  72.     Vector3.ClampMagnitude(velocityChange, maxForce);
  73.  
  74.     PredictionRigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
  75.  
  76.     if (data.Jump)
  77.     {
  78.         Vector3 jmpFrc = new Vector3(0, _jumpForce, 0);
  79.         PredictionRigidbody.AddForce(jmpFrc, ForceMode.Impulse);
  80.     }
  81.  
  82.     if (state != ReplicateState.ReplayedFuture && state != ReplicateState.CurrentFuture)
  83.     {
  84.         var oldRotation = transform.eulerAngles;
  85.         transform.rotation = Quaternion.Euler(oldRotation.x, data.LookRotationY, oldRotation.z);
  86.     }
  87.  
  88.  
  89.     PredictionRigidbody.Simulate();
  90. }
  91.  
  92.  
  93. public override void CreateReconcile()
  94. {
  95.     ReconcileData rd = new ReconcileData(PredictionRigidbody);
  96.     ReconcileState(rd);
  97. }
  98.  
  99.  
  100. [Reconcile]
  101. private void ReconcileState(ReconcileData data, Channel channel = Channel.Unreliable)
  102. {
  103.     PredictionRigidbody.Reconcile(data.PredictionRigidbody);
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement