From 48f13ca4fae6d1fc383f1347f28764c772b01f42 Mon Sep 17 00:00:00 2001 From: Pascal Serrarens Date: Mon, 15 Apr 2024 16:55:13 +0200 Subject: [PATCH] Instantiating is working --- .../Resources/HumanoidPlayer.prefab | 2 +- .../Netcode/HumanoidPlayerNetcode.cs | 108 ++++++++++++------ .../Networking/Netcode/NetcodeStarter.cs | 4 +- 3 files changed, 74 insertions(+), 40 deletions(-) diff --git a/Runtime/HumanoidControl/Prefabs/Networking/Resources/HumanoidPlayer.prefab b/Runtime/HumanoidControl/Prefabs/Networking/Resources/HumanoidPlayer.prefab index 29488d2..0531ea3 100644 --- a/Runtime/HumanoidControl/Prefabs/Networking/Resources/HumanoidPlayer.prefab +++ b/Runtime/HumanoidControl/Prefabs/Networking/Resources/HumanoidPlayer.prefab @@ -71,4 +71,4 @@ MonoBehaviour: _syncTracking: 0 _debug: 1 _sendRate: 25 - _createLocalRemotes: 1 + _createLocalRemotes: 0 diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs b/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs index 88c30f6..36e28dc 100644 --- a/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode/HumanoidPlayerNetcode.cs @@ -40,7 +40,11 @@ namespace Passer.Humanoid { // end temporary dummies public ulong nwId { - get { return identity.NetworkObjectId; } + get { + if (identity == null) + identity = GetComponent(); + return identity.NetworkObjectId; + } } [SerializeField] @@ -70,11 +74,11 @@ namespace Passer.Humanoid { Unity.Netcode.NetworkObject nwObject = GetNetworkObject(objIdentity); if (nwObject == null) { // Player Objects - nwObject = NetworkManager.Singleton.ConnectedClients[objIdentity].PlayerObject; - if (nwObject != null) - return nwObject.gameObject; - else - return null; + //nwObject = NetworkManager.Singleton.ConnectedClients[objIdentity].PlayerObject; + //if (nwObject != null) + // return nwObject.gameObject; + //else + return null; } GameObject gameObject = nwObject.gameObject; return gameObject; @@ -89,26 +93,52 @@ namespace Passer.Humanoid { set { _createLocalRemotes = value; } } protected Vector3 _localRemotePosition = new(0, 0, -1); - public Vector3 localRemotePosition { get { return _localRemotePosition; } } + public Vector3 localRemotePosition { get { return _localRemotePosition; } } #region Init - public void Start() { + public void Awake() { if (identity == null) identity = GetComponent(); humanoids = HumanoidNetworking.FindLocalHumanoids(); - if (debug <= HumanoidNetworking.DebugLevel.Info) - DebugLog("Found " + humanoids.Count + " Humanoids"); + DebugLog($"Found {humanoids.Count} Humanoids"); + + //if (identity.IsLocalPlayer) { + // isLocal = true; + // name = "HumanoidPlayer(Local)"; + + // humanoids = HumanoidNetworking.FindLocalHumanoids(); + // if (debug <= HumanoidNetworking.DebugLevel.Info) + // DebugLog($"Found {humanoids.Count} Humanoids"); + //} + //DebugLog($"awakened {this}"); + } + + public void Start() { + if (identity.IsLocalPlayer == false) + return; + + isLocal = true; + name = "HumanoidPlayer(Local)"; + + //humanoids = HumanoidNetworking.FindLocalHumanoids(); + //if (debug <= HumanoidNetworking.DebugLevel.Info) + // DebugLog($"Found {humanoids.Count} Humanoids"); for (int i = 0; i < humanoids.Count; i++) { HumanoidControl humanoid = humanoids[i]; - if (humanoid.isRemote || createLocalRemotes == false) + if (humanoid.isRemote) // || createLocalRemotes == false) continue; + humanoid.nwId = identity.NetworkObjectId; humanoid.humanoidNetworking = this; + + if (debug <= HumanoidNetworking.DebugLevel.Info) + Debug.Log(humanoid.nwId + ": Send Start Humanoid " + humanoid.humanoidId); + ((IHumanoidNetworking)this).InstantiateHumanoid(humanoid); } } @@ -144,39 +174,39 @@ namespace Passer.Humanoid { #endregion - #region Start + //#region Start - public virtual void OnStartLocalPlayer() { - isLocal = true; - name = "HumanoidPlayer(Local)"; + //public virtual void OnStartLocalPlayer() { + // isLocal = true; + // name = "HumanoidPlayer(Local)"; - humanoids = HumanoidNetworking.FindLocalHumanoids(); - if (debug <= HumanoidNetworking.DebugLevel.Info) - Debug.Log((int)identity.NetworkObjectId + ": Found " + humanoids.Count + " Humanoids"); + // humanoids = HumanoidNetworking.FindLocalHumanoids(); + // if (debug <= HumanoidNetworking.DebugLevel.Info) + // Debug.Log((int)identity.NetworkObjectId + ": Found " + humanoids.Count + " Humanoids"); - for (int i = 0; i < humanoids.Count; i++) { - HumanoidControl humanoid = humanoids[i]; - if (humanoid.isRemote) - continue; + // for (int i = 0; i < humanoids.Count; i++) { + // HumanoidControl humanoid = humanoids[i]; + // if (humanoid.isRemote) + // continue; - humanoid.nwId = identity.NetworkObjectId; - humanoid.humanoidNetworking = this; + // humanoid.nwId = identity.NetworkObjectId; + // humanoid.humanoidNetworking = this; - if (debug <= HumanoidNetworking.DebugLevel.Info) - Debug.Log(humanoid.nwId + ": Send Start Humanoid " + humanoid.humanoidId); + // if (debug <= HumanoidNetworking.DebugLevel.Info) + // Debug.Log(humanoid.nwId + ": Send Start Humanoid " + humanoid.humanoidId); - ((IHumanoidNetworking)this).InstantiateHumanoid(humanoid); - } - foreach (HumanoidControl humanoid in humanoids) - DetectNetworkObjects(humanoid); + // ((IHumanoidNetworking)this).InstantiateHumanoid(humanoid); + // } + // //foreach (HumanoidControl humanoid in humanoids) + // // DetectNetworkObjects(humanoid); - NetworkingSpawner spawner = FindObjectOfType(); - if (spawner != null) - spawner.OnNetworkingStarted(); - } + // //NetworkingSpawner spawner = FindObjectOfType(); + // //if (spawner != null) + // // spawner.OnNetworkingStarted(); + //} - #endregion + //#endregion #region Update @@ -298,7 +328,7 @@ namespace Passer.Humanoid { } - [Rpc(SendTo.NotServer)] // @ remote client + [Rpc(SendTo.NotOwner)] // @ remote client protected virtual void ReceiveInitiateHumanoidRpc(byte[] data) { HumanoidNetworking.InstantiateHumanoid instantiateHumanoid = new HumanoidNetworking.InstantiateHumanoid(data); if (debug <= HumanoidNetworking.DebugLevel.Info) @@ -595,9 +625,13 @@ namespace Passer.Humanoid { #region Debug public void DebugLog(string message) { - Debug.Log(identity.NetworkObjectId + ": " + message); + if (identity == null) + Debug.Log("??: " + message); + else + Debug.Log(identity.NetworkObjectId + ": " + message); } + public void DebugWarning(string message) { Debug.LogWarning(identity.NetworkObjectId + ": " + message); } diff --git a/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs b/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs index 1fbf565..14b4d7d 100644 --- a/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs +++ b/Runtime/HumanoidControl/Scripts/Networking/Netcode/NetcodeStarter.cs @@ -35,12 +35,12 @@ namespace Passer { void INetworkingStarter.StartClient(NetworkingStarter nwStarter) { Debug.Log("start Netcode Client"); //NetworkManager networkManager = nwStarter.GetComponent(); - //NetworkClient nwClient = - NetworkManager.Singleton.StartClient(); NetworkManager.Singleton.GetComponent().SetConnectionData( nwStarter.serverIpAddress, nwStarter.serverPort ); + //NetworkClient nwClient = + NetworkManager.Singleton.StartClient(); } public static void StartClient(string serverIpAddress, ushort serverPort) {