|
| 1 | +using UnityEngine; |
| 2 | + |
| 3 | +/// <summary> |
| 4 | +/// Add this component to any rigidbodies that need to exist in a scene with Server Side prediction |
| 5 | +/// It handles smoothing the rigidbody and stopping its collisions during replays |
| 6 | +/// TODO: In the future we may add support for replaying these rigidbodies during replays |
| 7 | +/// </summary> |
| 8 | +[RequireComponent(typeof(Rigidbody))] |
| 9 | +public class AirshipPredictionRigidbodyController : MonoBehaviour, IPredictedReplay { |
| 10 | + public Rigidbody rigid; |
| 11 | + [Tooltip("The visual component of the object that will be smoothed inbetween physics steps")] |
| 12 | + public Transform graphicsHolder; |
| 13 | + |
| 14 | + public string friendlyName => "Rigidbody_" + gameObject.name; |
| 15 | + |
| 16 | + public float guid => this.GetInstanceID(); |
| 17 | + |
| 18 | + private bool waiting = false; |
| 19 | + private bool wasKinematic = false; |
| 20 | + private bool wasColliding = false; |
| 21 | + private CollisionDetectionMode pastDetectionMode = CollisionDetectionMode.Discrete; |
| 22 | + |
| 23 | + private void Awake() { |
| 24 | + if(!rigid) { |
| 25 | + rigid = gameObject.GetComponent<Rigidbody>(); |
| 26 | + } |
| 27 | + if(!graphicsHolder) { |
| 28 | + Debug.LogWarning("Rigidbody is missing graphics holder and won't be smoothed or handled by client side prediction. GameObject: " + gameObject.name); |
| 29 | + } |
| 30 | + } |
| 31 | + |
| 32 | + private void OnEnable() { |
| 33 | + if(rigid && graphicsHolder){ |
| 34 | + //Register for rigidbody smoothing |
| 35 | + AirshipPredictionManager.instance.RegisterRigidbody(rigid, graphicsHolder); |
| 36 | + |
| 37 | + //Listen to replays |
| 38 | + AirshipPredictionManager.instance.RegisterPredictedObject(this); |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + private void OnDisable() { |
| 43 | + AirshipPredictionManager.instance.UnRegisterRigidbody(rigid); |
| 44 | + AirshipPredictionManager.instance.UnRegisterPredictedObject(this); |
| 45 | + } |
| 46 | + |
| 47 | + public void OnReplayStarted(AirshipPredictedState initialState, int historyIndex) { |
| 48 | + throw new System.NotImplementedException(); |
| 49 | + } |
| 50 | + |
| 51 | + public void OnReplayTickStarted(int tick) { |
| 52 | + throw new System.NotImplementedException(); |
| 53 | + } |
| 54 | + |
| 55 | + public void OnReplayTickFinished(int tick) { |
| 56 | + throw new System.NotImplementedException(); |
| 57 | + } |
| 58 | + |
| 59 | + public void OnReplayFinished(AirshipPredictedState initialState) { |
| 60 | + throw new System.NotImplementedException(); |
| 61 | + } |
| 62 | + |
| 63 | + public void OnReplayingOthersStarted() { |
| 64 | + if(waiting){ |
| 65 | + return; |
| 66 | + } |
| 67 | + waiting = true; |
| 68 | + print("Stopping this rigidbody"); |
| 69 | + this.wasColliding = this.rigid.detectCollisions; |
| 70 | + this.wasKinematic = this.rigid.isKinematic; |
| 71 | + this.pastDetectionMode = this.rigid.collisionDetectionMode; |
| 72 | + this.rigid.isKinematic = true; |
| 73 | + this.rigid.detectCollisions = false; |
| 74 | + } |
| 75 | + |
| 76 | + public void OnReplayingOthersFinished() { |
| 77 | + if(!waiting){ |
| 78 | + return; |
| 79 | + } |
| 80 | + waiting = false; |
| 81 | + print("Starting this rigidbody back up"); |
| 82 | + this.rigid.isKinematic = this.wasKinematic; |
| 83 | + this.rigid.detectCollisions = this.wasColliding; |
| 84 | + } |
| 85 | +} |
0 commit comments