PredictedRigidbody.cs

This commit is contained in:
mischa 2023-09-01 15:58:04 +02:00
parent 374583aa90
commit 0ee4e7d374
5 changed files with 79 additions and 0 deletions

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 09cc6745984c453a8cfb4cf4244d2570
timeCreated: 1693576410

View File

@ -0,0 +1,33 @@
using UnityEngine;
namespace Mirror.PredictedRigidbody
{
[RequireComponent(typeof(Rigidbody))]
public class PredictedRigidbody : NetworkBehaviour
{
Rigidbody rb;
void Awake()
{
rb = GetComponent<Rigidbody>();
}
// send state to clients every sendInterval.
// reliable for now.
public override void OnSerialize(NetworkWriter writer, bool initialState)
{
writer.WriteVector3(rb.position);
writer.WriteVector3(rb.velocity);
}
// read the server's state, but don't apply it directly.
// this is where reconciliation happens if necessary.
public override void OnDeserialize(NetworkReader reader, bool initialState)
{
double timestamp = connectionToServer.remoteTimeStamp;
Vector3 position = reader.ReadVector3();
Vector3 velocity = reader.ReadVector3();
Debug.Log($"OnDeserialize: {timestamp} {position} {velocity}");
}
}
}

View File

@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: d38927cdc6024b9682b5fe9778b9ef99
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {fileID: 2800000, guid: 7453abfe9e8b2c04a8a47eb536fe21eb, type: 3}
userData:
assetBundleName:
assetBundleVariant:

View File

@ -15,6 +15,7 @@ GameObject:
- component: {fileID: 3429911415116987811}
- component: {fileID: -177125271246800426}
- component: {fileID: 5308121378143249733}
- component: {fileID: -8731861437394929722}
m_Layer: 0
m_Name: RedPredicted
m_TagString: Untagged
@ -148,3 +149,18 @@ MonoBehaviour:
syncDirection: 0
syncMode: 0
syncInterval: 0
--- !u!114 &-8731861437394929722
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3429911415116987808}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d38927cdc6024b9682b5fe9778b9ef99, type: 3}
m_Name:
m_EditorClassIdentifier:
syncDirection: 0
syncMode: 0
syncInterval: 0

View File

@ -146,6 +146,7 @@ GameObject:
- component: {fileID: 3429911415116987811}
- component: {fileID: 1848203816128897140}
- component: {fileID: 6607303410184343467}
- component: {fileID: -2068612481632719751}
m_Layer: 0
m_Name: WhitePredicted
m_TagString: Untagged
@ -283,3 +284,18 @@ MonoBehaviour:
dragIndicator: {fileID: 982362982}
rigidBody: {fileID: 1848203816128897140}
forceMultiplier: 100
--- !u!114 &-2068612481632719751
MonoBehaviour:
m_ObjectHideFlags: 0
m_CorrespondingSourceObject: {fileID: 0}
m_PrefabInstance: {fileID: 0}
m_PrefabAsset: {fileID: 0}
m_GameObject: {fileID: 3429911415116987808}
m_Enabled: 1
m_EditorHideFlags: 0
m_Script: {fileID: 11500000, guid: d38927cdc6024b9682b5fe9778b9ef99, type: 3}
m_Name:
m_EditorClassIdentifier:
syncDirection: 0
syncMode: 0
syncInterval: 0