- Home /
spawning and moving object in multiplayer
I'm trying to do this thing where player picks up object from the game map/ current scene. it's instantiated as copy and parent to the player that picked it up. Or so I think it works. Then that player is able to carry that object anywhere and then place it back to the scene. it should be shown to other player too that carrying player is carrying the object. In my game happens so that the the host client is able to carry object correctly but if I run another instance from "run and build" and set it to as client, it now can carry things but whenever he moves, the object it's carrying is lagging behind. but for the host it moves accurately over the other player that's carrying it.
I call these functions from Update() function if player presses 'E' button.
// drop item at position
[Command]
void CmdDropHeldItemAt(int x, int y)
{
RaycastHit2D hit = Physics2D.Raycast(new Vector2(x, y), Vector2.zero);
if(hit.collider == null) // if didn't hit anything, there's nothing on the way.
{
GameObject holdedObject = GameObject.Instantiate(heldItem); // make a copy
Destroy(heldItem); // and delete the original
holdedObject.transform.parent = map.GetComponent<MapGenerator>().tiles_parent.transform;
holdedObject.transform.localPosition = new Vector3(0, 0, 0);
holdedObject.transform.position = new Vector3(x, y, 0);
holdedObject.GetComponent<BoxCollider2D>().enabled = true;
holdedObject.GetComponent<NetworkIdentity>().localPlayerAuthority = false;
holdedObject.GetComponent<NetworkTransform>().enabled = false;
NetworkServer.Spawn(holdedObject); // spawn the gameobject for clients.
holdingItem = false;
return;
} else
{
Debug.Log("hitted: " + hit.collider.name);
}
Debug.Log("Can't drop item here. x=" + x + ", y=" + y);
return;
}
// pickup item at position
[Command]
void CmdPickupItemAt(int x, int y)
{
RaycastHit2D hit = Physics2D.Raycast(new Vector2(x, y), Vector2.zero);
if (hit.collider != null && hit.collider.tag == "heldOverHead")
{
heldItem = GameObject.Instantiate(hit.collider.gameObject); // make a copy
Destroy(hit.collider.gameObject); // and delete the original
heldItem.transform.parent = transform;
heldItem.transform.localPosition = new Vector3(0, 0.8f, 0);
heldItem.GetComponent<BoxCollider2D>().enabled = false;
heldItem.GetComponent<NetworkTransform>().enabled = true;
heldItem.GetComponent<NetworkIdentity>().localPlayerAuthority = true;
NetworkServer.SpawnWithClientAuthority(heldItem, connectionToClient); // spawn to server too, so other see it.
holdingItem = true;
return;
}
Debug.Log("Can't pickup item here. x=" + x + ", y=" + y);
return;
}
also do I need this somewhere? probably yes.
if (!isLocalPlayer)
{
return;
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612143159im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
can't get clients to spawn object 0 Answers
Networking - Spawning non Player Object 0 Answers
Why are objects only spawning on host but not on remote clients? 3 Answers
Check when a player finished spawning [Netcode For GameObjects!] 2 Answers
When the game starts, how can I instantiate a ball onto a random client? 0 Answers