您最多选择25个主题
主题必须以中文或者字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
58 行
2.0 KiB
58 行
2.0 KiB
using Unity.Netcode;
|
|
using UnityEngine;
|
|
|
|
namespace LobbyRelaySample.inGame
|
|
{
|
|
[RequireComponent(typeof(Rigidbody))]
|
|
public class SymbolContainer : NetworkBehaviour, IReceiveMessages
|
|
{
|
|
private Rigidbody m_rb;
|
|
private bool m_isConnected = false;
|
|
private bool m_hasGameStarted = false;
|
|
private void OnGameStarted()
|
|
{
|
|
m_hasGameStarted = true;
|
|
if (m_isConnected)
|
|
BeginMotion();
|
|
}
|
|
|
|
public void Start()
|
|
{
|
|
if (!IsHost)
|
|
{ this.enabled = false; // Just disabling this script.
|
|
return;
|
|
}
|
|
m_rb = this.GetComponent<Rigidbody>();
|
|
Locator.Get.Messenger.Subscribe(this);
|
|
GetComponent<NetworkObject>().Spawn();
|
|
}
|
|
|
|
public override void OnNetworkSpawn()
|
|
{
|
|
if (IsHost)
|
|
{
|
|
m_isConnected = true;
|
|
m_rb.MovePosition(Vector3.up * 10);
|
|
if (m_hasGameStarted)
|
|
BeginMotion();
|
|
}
|
|
}
|
|
|
|
private void BeginMotion()
|
|
{
|
|
// Note: The SymbolObjects, which will be children of this object, need their NetworkTransforms to have IsLocalSpace set to true. Otherwise, they might get desynced.
|
|
// (This would manifest as packet loss errors.)
|
|
// Also note: The initial position of the SymbolObject prefab is set to be outside the camera view in the Z-direction, so that it doesn't interpolate past the actual
|
|
// position when it spawns on a client (as opposed to in the Y-direction, since this SymbolContainer is also moving downward).
|
|
m_rb.velocity = Vector3.down;
|
|
}
|
|
|
|
public void OnReceiveMessage(MessageType type, object msg)
|
|
{
|
|
if (type == MessageType.GameBeginning)
|
|
{ Locator.Get.Messenger.Unsubscribe(this);
|
|
OnGameStarted();
|
|
}
|
|
}
|
|
}
|
|
}
|