networked game with simple pick up and throw physics

Hello,

I am trying to learn how to code a very simple game in C#. The players join a server and there is a cube in the center of the screen. Either player can pick up the cube and it hovers in front of them. They can then throw it at the other player. Right now I’ve got a simple 1-player pick up and throw script, but am having difficulty figuring out how to make it work for multiplayer.

It works okay while there’s only one client connected to the server, but as soon as a a second client connects, both clients version of the pickup cube is trying to tell everyone what’s going on with the same cube and it gets really jittery and messy.

Scripts:
PlayerPickupScript
ObjectMovementScript

So what I’m trying to figure out is how to fix this. Can I set the pickup cube to have an owner? Maybe ignore the information from all other clients?

I’m trying to wrap my head around how I would make this authoritative, but I might just be too new to programming to understand how to make that work.

Any advice would be GREATLY appreciated. Thank you!

Well without a 3rd party, such as a server that you host that calculates information and spits out a result to clients, you can’t really make it authoritative. You can give authority of objects to other players though and disallow non-owners to calculate updates if you wanted to. I personally use a unique int to differentiate between different players. I use an int since it’s cheap in bandwidth and etc. You could assign that int to a cube too and only allow clients to send updates about the cheap, or to calculate the physics for a cube, if it has that int id.

Might be best to create two different cubes though, local client cubes and networked client cubes. The local client cube could have a rigidbody on it and the networked cube could contain only a script that sets the positions that other players send to the client. There are a couple of ways to do this though.

Using two different cubes sounds like a smart plan. Any ideas on how to accomplish that? Not looking for specific code samples, just an overall direction or strategy.

Thanks for replying!