For a unity 3D multiplayer game, how to spawn and despawn multiple gameobjects for specific client?
30. Červenec 2024 v 17:01
I am working on a Unity 3D multiplayer game. There are 3 gameobjects (table, chair, pen) and apart from host, I have two client - student and teacher, consider them as two roles. I know how to instantiate a prefab and sync it across clients. But if i want to spawn-despawn one or two gameobjects for client 1 and not for client 2, how can I achieve that?
private void Update()
{
if (!IsOwner) return;
if (Input.GetKeyDown(KeyCode.T))
{
spawnedObjectTransform = Instantiate(spawnedObjectPrefab);
spawnedObjectTransform.GetComponent<NetworkObject>().Spawn(true);
}
if (Input.GetKeyDown(KeyCode.Y))
{
Destroy(spawnedObjectTransform.gameObject);
}
Vector3 moveDir = new Vector3(0, 0, 0);
if (Input.GetKey(KeyCode.W)) moveDir.z = +1f;
if (Input.GetKey(KeyCode.S)) moveDir.z = -1f;
if (Input.GetKey(KeyCode.A)) moveDir.x = -1f;
if (Input.GetKey(KeyCode.D)) moveDir.x = +1f;
float moveSpeed = 3f;
transform.position += moveDir * moveSpeed * Time.deltaTime;
}
Or is there any other alternative like defining roles for clients and then setting permissions in the scene based on that role and again how can I do that?
Thanks in advance!