private void LateUpdate() { if (State != null) { State.RigidBodyState = (Rigidbody != null) ? RigidBodyState.FromRigidbody(Rigidbody) : new RigidBodyState(); } }
public void Pause() { if (isPaused) { return; } state = new RigidBodyState(rigidbody); RigidBodyState.Paused.Restore(rigidbody); isPaused = true; }
// Apply updated properties. private void ProcessProperty(uint actorId, ActorStateProperty asp) { // Only properties with defined cases are actually used. switch (asp.PropertyName) { // Rigidbody state. // Includes position, rotation, and velocity info (see RigidBodyState for all fields). case "TAGame.RBActor_TA:ReplicatedRBState": RigidBodyState rbState = (RigidBodyState)(asp.Data); Vector3 pos = new Vector3(rbState.Position.X, rbState.Position.Z, rbState.Position.Y) * posScale; Vector3 rot = new Vector3(rbState.Rotation.X, rbState.Rotation.Y, rbState.Rotation.Z) * rotScale; rlObjects[actorId].transform.position = pos; rlObjects[actorId].transform.eulerAngles = rot; break; // Player name. case "Engine.PlayerReplicationInfo:PlayerName": rlObjects[actorId].name += " (" + (string)asp.Data + ")"; break; // Team / color info. case "TAGame.Car_TA:TeamPaint": // Make object orange if team 0, blue if team 1. MeshRenderer meshRend = rlObjects[actorId].GetComponent <MeshRenderer>(); TeamPaint tp = (TeamPaint)asp.Data; if (tp.TeamNumber == 0) { meshRend.material.color = new Color(1, 0.5f, 0); } else { meshRend.material.color = Color.blue; } break; } // Add property to PropertyInfo component for debuging. rlObjects[actorId].GetComponent <PropertyInfo>().ProcessProperty(asp); }
public static void ApplyRigidbodyState(RigidBodyState newRigidBodyState, RigidBodyState oldRigidBodyState, Rigidbody rigidbody, float roundTripTime) { // Correct position. var correctedPosition = CorrectedPosition( newRigidBodyState.Position, newRigidBodyState.Velocity, roundTripTime, rigidbody.transform.position ); rigidbody.transform.position = correctedPosition; newRigidBodyState.Position = correctedPosition; // Correct orientation. var correctedEulerAngles = CorrectedEulerAngles( newRigidBodyState.EulerAngles, newRigidBodyState.AngularVelocity, roundTripTime, rigidbody.transform.eulerAngles ); rigidbody.transform.eulerAngles = correctedEulerAngles; newRigidBodyState.EulerAngles = correctedEulerAngles; // Correct velocity. var correctedVelocity = CorrectedVelocity( newRigidBodyState.Velocity, roundTripTime, rigidbody.velocity ); rigidbody.velocity = correctedVelocity; newRigidBodyState.Velocity = correctedVelocity; // Correct angular velocity. var correctedAngularVelocity = CorrectedAngularVelocity( newRigidBodyState.AngularVelocity, roundTripTime, rigidbody.angularVelocity ); rigidbody.angularVelocity = correctedAngularVelocity; newRigidBodyState.AngularVelocity = correctedAngularVelocity; }
private void OnPhotonSerializeView(PhotonStream Stream, PhotonMessageInfo Info) { // Send data to server if (Stream.isWriting) { // Not sending data when in slow-motion or fast-motion. if (Time.timeScale != NormalTimeScale) return; var Position = rigidbody.position; var Rotation = rigidbody.rotation; var Velocity = rigidbody.velocity; var AngularVelocity = rigidbody.angularVelocity; Stream.Serialize(ref Position); Stream.Serialize(ref Velocity); Stream.Serialize(ref Rotation); Stream.Serialize(ref AngularVelocity); } // Read data from remote client else { var Position = Vector3.zero; var Velocity = Vector3.zero; var Rotation = Quaternion.identity; var AngularVelocity = Vector3.zero; Stream.Serialize(ref Position); Stream.Serialize(ref Velocity); Stream.Serialize(ref Rotation); Stream.Serialize(ref AngularVelocity); // Shift the buffer sideways, deleting the last state. for (var i = BufferedStates.Length - 1; i >= 1; i--) BufferedStates[i] = BufferedStates[i - 1]; // Record current state in slot 0 BufferedStates[0] = new RigidBodyState(Info.timestamp, Position, Velocity, Rotation, AngularVelocity); // Update used slot count, however never exceed the buffer size // Slots aren't actually freed so this just makes sure the buffer is // filled up and that uninitalized slots aren't used. UsedBufferedStatesCount = Mathf.Min(UsedBufferedStatesCount + 1, BufferedStates.Length); // Check if states are in order, if it is inconsistent you could reshuffel or // drop the out-of-order state. Nothing is done here for (var i = 0; i < UsedBufferedStatesCount - 1; i++) { if (BufferedStates[i].Timestamp < BufferedStates[i + 1].Timestamp) Debug.Log("State inconsistent"); } } }