oumuamua step 1: raw unreliable

This commit is contained in:
vis2k 2021-03-15 10:47:57 +08:00
parent c957500710
commit 19db6b5c69
7 changed files with 154 additions and 0 deletions

View File

@ -0,0 +1,3 @@
fileFormatVersion: 2
guid: 08e0fc21b95a46ddbdef2dd29f16baae
timeCreated: 1615775769

View File

@ -0,0 +1,10 @@
using UnityEngine;
namespace Mirror.Experimental
{
[DisallowMultipleComponent]
public class Oumuamua : OumuamuaBase
{
protected override Transform targetComponent => transform;
}
}

View File

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

View File

@ -0,0 +1,95 @@
// based on Glenn Fielder https://gafferongames.com/post/snapshot_interpolation/
//
// Base class for NetworkTransform and NetworkTransformChild.
// => simple unreliable sync without any interpolation for now.
// => which means we don't need teleport detection either
using UnityEngine;
namespace Mirror.Experimental
{
public abstract class OumuamuaBase : NetworkBehaviour
{
[Header("Authority")]
[Tooltip("Set to true if moves come from owner client, set to false if moves always come from server")]
public bool clientAuthority;
// Is this a client with authority over this transform?
// This component could be on the player object or any object that has been assigned authority to this client.
bool IsClientWithAuthority => hasAuthority && clientAuthority;
// target transform to sync. can be on a child.
protected abstract Transform targetComponent { get; }
// send interval: send frequently (unreliable, no interpolation)
[Range(0, 1)] public float sendInterval = 0.050f;
float lastClientSendTime;
float lastServerSendTime;
// local authority client sends sync message to server for broadcasting
[Command(channel = Channels.DefaultUnreliable)]
void CmdClientToServerSync(Vector3 localPosition, Quaternion localRotation, Vector3 localScale)
{
// apply if in client authority mode
if (clientAuthority)
{
ApplyPositionRotationScale(localPosition, localRotation, localScale);
}
}
// server broadcasts sync message to all clients
[ClientRpc(channel = Channels.DefaultUnreliable)]
void RpcServerToClientSync(Vector3 localPosition, Quaternion localRotation, Vector3 localScale)
{
// apply for all objects except local player with authority
if (!IsClientWithAuthority)
{
ApplyPositionRotationScale(localPosition, localRotation, localScale);
}
}
// set position carefully depending on the target component
void ApplyPositionRotationScale(Vector3 position, Quaternion rotation, Vector3 scale)
{
// local position/rotation for VR support
targetComponent.localPosition = position;
targetComponent.localRotation = rotation;
targetComponent.localScale = scale;
}
void Update()
{
// if server then always sync to others.
if (isServer)
{
// check only each 'sendInterval'
if (Time.time - lastServerSendTime >= sendInterval)
{
// broadcast to clients
RpcServerToClientSync(targetComponent.localPosition,
targetComponent.localRotation,
targetComponent.localScale);
lastServerSendTime = Time.time;
}
}
// no 'else if' since host mode would be both
if (isClient)
{
// send to server if we have local authority (and aren't the server)
// -> only if connectionToServer has been initialized yet too
if (!isServer && IsClientWithAuthority)
{
// check only each 'sendInterval'
if (Time.time - lastClientSendTime >= sendInterval)
{
// send to server
CmdClientToServerSync(targetComponent.localPosition,
targetComponent.localRotation,
targetComponent.localScale);
lastClientSendTime = Time.time;
}
}
}
}
}
}

View File

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

View File

@ -0,0 +1,13 @@
// A component to synchronize the position of child transforms of networked objects.
// There must be a NetworkTransform on the root object of the hierarchy. There can be multiple NetworkTransformChild components on an object. This does not use physics for synchronization, it simply synchronizes the localPosition and localRotation of the child transform and lerps towards the recieved values.
using UnityEngine;
namespace Mirror.Experimental
{
public class OumuamuaChild : OumuamuaBase
{
[Header("Target")]
public Transform target;
protected override Transform targetComponent => target;
}
}

View File

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