Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // DATA STRUCTURES
- public struct ReplicateData : IReplicateData
- {
- public bool Jump;
- public float Horizontal;
- public float Vertical;
- public float LookRotationY;
- public ReplicateData(bool jump, float horizontal, float vertical, float lookrotationy) : this()
- {
- Jump = jump;
- Horizontal = horizontal;
- Vertical = vertical;
- LookRotationY = lookrotationy;
- _tick = 0;
- }
- private uint _tick;
- public void Dispose() { }
- public uint GetTick() => _tick;
- public void SetTick(uint value) => _tick = value;
- }
- public struct ReconcileData : IReconcileData
- {
- public PredictionRigidbody PredictionRigidbody;
- public ReconcileData(PredictionRigidbody pr) : this()
- {
- PredictionRigidbody = pr;
- }
- private uint _tick;
- public void Dispose() { }
- public uint GetTick() => _tick;
- public void SetTick(uint value) => _tick = value;
- }
- private ReplicateData CreateReplicateData()
- {
- if (!base.IsOwner)
- {
- return default;
- }
- float horizontal = Input.GetAxisRaw("Horizontal");
- float vertical = Input.GetAxisRaw("Vertical");
- float lookRotationy = lookRotationY;
- ReplicateData md = new ReplicateData(_jump, horizontal, vertical, lookRotationy);
- _jump = false;
- return md;
- }
- [Replicate]
- void RunInputs(ReplicateData data, ReplicateState state = ReplicateState.Invalid, Channel channel = Channel.Unreliable)
- {
- if (!base.IsOwner)
- {
- return;
- }
- Vector3 currentVelocity = PredictionRigidbody.Rigidbody.velocity;
- Vector3 targetVelocity = new Vector3(data.Horizontal, 0, data.Vertical) * _moveRate;
- targetVelocity = transform.TransformDirection(targetVelocity);
- Vector3 velocityChange = targetVelocity - currentVelocity;
- velocityChange = new Vector3(velocityChange.x, 0, velocityChange.z);
- Vector3.ClampMagnitude(velocityChange, maxForce);
- PredictionRigidbody.AddForce(velocityChange, ForceMode.VelocityChange);
- if (data.Jump)
- {
- Vector3 jmpFrc = new Vector3(0, _jumpForce, 0);
- PredictionRigidbody.AddForce(jmpFrc, ForceMode.Impulse);
- }
- if (state != ReplicateState.ReplayedFuture && state != ReplicateState.CurrentFuture)
- {
- var oldRotation = transform.eulerAngles;
- transform.rotation = Quaternion.Euler(oldRotation.x, data.LookRotationY, oldRotation.z);
- }
- PredictionRigidbody.Simulate();
- }
- public override void CreateReconcile()
- {
- ReconcileData rd = new ReconcileData(PredictionRigidbody);
- ReconcileState(rd);
- }
- [Reconcile]
- private void ReconcileState(ReconcileData data, Channel channel = Channel.Unreliable)
- {
- PredictionRigidbody.Reconcile(data.PredictionRigidbody);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement