I have a helicopter vehicle in my scene which is instantiated via Network.Instantiate.
It has a rigidbody, networkview, and networkrigidbody script as the observed property of my networkview. And of course, there is also the helicopter control script.
The issue I am having is, if the server player gets into the helicopter, he can move the helicopter around via the helicopter control script. But if the client gets into the helicopter, he cannot control it and it does not move. I can’t figure out how to make this work properly.
Here is the relevant part of my helicopter control code:
Who owns the objects ? If it’s the server, then the server will be the only one that can move the object. Otherwise you will need to delegate control in some way.
Well at first I was using Network.Instantiate on the server side. But now, I had created a list on the server side of the objects that need to be instantiated, currently there is only the helicopter in that list. I am doing something like this:
Thinking that if each player instantiates the object themselves, they’ll be able to control it… unfortunately that did not work. The server is the only one that can actually still “fly” it. I am basically stuck on how to delegate control of the helicopter.
You need to use Network.Instantiate so the networkview is set up to have an owner (the person who calls Network.Instantiate), or configure the viewId yourself. You’ll either need to re-network.Instantiate the helicopter whenever someone new gets in it, or have players other than the server send their helicopter input as messages to the server (assuming the server owns it) and have the server process the input and move the helicopter, broadcasting the results with networkrigidbody.
Hm, ok so I will configure the viewID myself for the networkview via Network.AllocateViewID() on the server end when the helicopter get’s instantiated.
When you say:
Are you saying to destroy the gameobject when someone new gets in, then Network.Instantiate it on their client again so that they own it? If that is the case, I will probably go with:
But my question for that is, since my helicopter’s direction is controlled by the mouse movement and the left and right rotation is controlled by the keyboard (“A” and “D” keys), how do I send that input to the server?
Ok so I just figured out that if any player gets into the vehicle, they allocate a new networkviewid to the helicopter and send that out via RPC to all, so that all players update it to that viewID. It looks like it’s working fine now, almost seems kind of a hacky way to do it, will this work in the long run?
I did notice an error, for example “View ID AllocatedID: 7 not found during lookup. Strange behaviour may occur”. I assume it’s because the server sets the networkviewID then sends it out to the clients, so they all don’t get the update at the sametime therefore that error shows up. I guess I can stop sending out state synchronization from that networkview and then I can do some sort of acknowledgement from all players when they set their new networkviewID to the helicopter, then resume send data from it.