update
This commit is contained in:
@@ -14,9 +14,8 @@ namespace Hallucinate.Network
|
||||
{
|
||||
public override void Spawned()
|
||||
{
|
||||
// Because we passed State Authority to the client in BasicSpawner,
|
||||
// HasStateAuthority is true ONLY for the local player.
|
||||
bool isLocal = Object.HasStateAuthority;
|
||||
// We use InputAuthority to identify the local player because this is Host mode.
|
||||
bool isLocal = Object.HasInputAuthority;
|
||||
|
||||
// 1. Isolate Input: Only the local player should read keyboard/mouse inputs.
|
||||
var locoHandler = GetComponent<UltimateCharacterLocomotionHandler>();
|
||||
@@ -33,26 +32,68 @@ namespace Hallucinate.Network
|
||||
{
|
||||
cameraController.Character = gameObject;
|
||||
}
|
||||
|
||||
// CRITICAL: Disable NetworkTransform on the local client so Fusion doesn't pull the character back!
|
||||
var nt = GetComponent<NetworkTransform>();
|
||||
if (nt != null) nt.enabled = false;
|
||||
}
|
||||
}
|
||||
|
||||
// --- INetworkInfo Implementation ---
|
||||
|
||||
public bool IsLocalPlayer() => Object.HasStateAuthority;
|
||||
// We use InputAuthority to identify the local player in Host/Client mode
|
||||
public bool IsLocalPlayer() => Object.HasInputAuthority;
|
||||
public bool IsServer() => Runner.IsServer;
|
||||
|
||||
// Return FALSE because we are doing Client-Authoritative movement!
|
||||
public bool IsServerAuthoritative() => false;
|
||||
public bool IsServerAuthoritative() => false;
|
||||
|
||||
// --- Client-Auth Movement Sync via RPC ---
|
||||
// Since the Server holds State Authority, the client must send its transform to the server.
|
||||
// The server then broadcasts it to everyone else via NetworkTransform.
|
||||
|
||||
// --- INetworkCharacter Implementation ---
|
||||
// Since we are using Fusion's NetworkTransform to sync position,
|
||||
// we can leave these methods empty. Opsive will handle the local
|
||||
// movement, and Fusion's NetworkTransform will drag the remote players.
|
||||
|
||||
public void SetPosition(Vector3 position, bool snapAnimator) { }
|
||||
public void SetRotation(Quaternion rotation, bool snapAnimator) { }
|
||||
public void SetPositionAndRotation(Vector3 position, Quaternion rotation, bool snapAnimator) { }
|
||||
public override void FixedUpdateNetwork()
|
||||
{
|
||||
if (Object.HasInputAuthority)
|
||||
{
|
||||
RPC_UpdateTransform(transform.position, transform.rotation);
|
||||
}
|
||||
}
|
||||
|
||||
public override void Render()
|
||||
{
|
||||
// If this is a remote proxy, Fusion's NetworkTransform will update the Unity transform.
|
||||
// But Opsive maintains its own internal position and will snap it back unless we tell it to update!
|
||||
if (!Object.HasInputAuthority)
|
||||
{
|
||||
var loco = GetComponent<UltimateCharacterLocomotion>();
|
||||
if (loco != null)
|
||||
{
|
||||
// Feed the NetworkTransform's current position to Opsive so it knows we moved,
|
||||
// which also allows the Animator to play the correct movement animations!
|
||||
loco.SetPositionAndRotation(transform.position, transform.rotation, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
[Rpc(RpcSources.InputAuthority, RpcTargets.StateAuthority)]
|
||||
private void RPC_UpdateTransform(Vector3 pos, Quaternion rot)
|
||||
{
|
||||
// The Server receives this and applies it. NetworkTransform then syncs it to all other clients.
|
||||
transform.position = pos;
|
||||
transform.rotation = rot;
|
||||
}
|
||||
|
||||
public void SetPosition(Vector3 position, bool snapAnimator)
|
||||
{
|
||||
if (Object.HasInputAuthority) transform.position = position;
|
||||
}
|
||||
public void SetRotation(Quaternion rotation, bool snapAnimator)
|
||||
{
|
||||
if (Object.HasInputAuthority) transform.rotation = rotation;
|
||||
}
|
||||
public void SetPositionAndRotation(Vector3 position, Quaternion rotation, bool snapAnimator)
|
||||
{
|
||||
if (Object.HasInputAuthority) transform.SetPositionAndRotation(position, rotation);
|
||||
}
|
||||
public void ResetRotationPosition() { }
|
||||
public void SetActive(bool active, bool uiEvent) { }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user