Unity-Technologies / Unity-Technologies/com.unity.netcode.gameobjects

Client NetworkObjects experience Awake, Start, OnEnable, and TriggerEnter, CollisionEnter at world 0,0,0 upon instantiation

未關閉
#3,553 3 則留言 2 個 reaction 已指派 1 人 在 GitHub 檢視

@NoelStephensUnity 已經在處理了。

開始於 2025年7月17日。

priority:medium stat:imported type:bug
主要語言
C#
星號
2.3k
分支
461
平均合併
3 天 16 小時
30 天內合併 PR
20

描述

Description

When a client spawns a NetworkObject via NetworkSpawnManage.InstantiateNetworkPrefab() there is no position or rotation data passed into UnityEngine.Object.Instantiate() and therefore the object is created at world 0,0,0. On a later line of code the position and rotation are assigned, after instantiation.

This means Awake, Start, and OnEnable are all called when at world 0,0,0 -- this is arguably expected and why you have OnNetworkSpawn.

However, OnTriggerEnter (and probably OnCollisionEnter) also fire for things at world 0,0,0. That problem is outside of the scope of the OnNetworkSpawn warmup in my opinion.

Developers should not have to write custom code to handle this problem when the position and rotation could simply be passed into the instantiation.

internal NetworkObject InstantiateNetworkPrefab(GameObject networkPrefab, uint prefabGlobalObjectIdHash, Vector3? position, Quaternion? rotation)
{
    Vector3 defaultPosition = position ?? Vector3.zero;
    Quaternion defaultRotation = rotation ?? Quaternion.identity;
    var networkObject = UnityEngine.Object.Instantiate(networkPrefab, defaultPosition, defaultRotation).GetComponent<NetworkObject>();
    // networkObject.transform.position = position ?? networkObject.transform.position;
    // networkObject.transform.rotation = rotation ?? networkObject.transform.rotation;
    networkObject.NetworkManagerOwner = NetworkManager;
    networkObject.PrefabGlobalObjectIdHash = prefabGlobalObjectIdHash;
    return networkObject;
}
Environment
  • OS: Windows 11
  • Unity Version: 6000.1.3f1
  • Netcode Version: 2.4.2
  • Netcode Topology: Client-Server

貢獻指南

開啟貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

評估

這個 Issue 還沒有評估資料。

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。