- Home /
Multiplayer Flashlight Issue
I have a multiplayer FPS where local player character has a flashlight made from a spotlight attached to a cylinder . The Remote Player Prefab also has the same flashlight. The flashlight is controlled by a simple on/off script.
When running the multiplayer test each player can switch on his flashlight and the flashlight projects a beam onto an object when viewed in that players game window. However the other players cannot observe the flashlight or it's beam.
I don't really have an in-depth understanding of networked multiplayer games but understand that the other players observe the RemotePlayer Prefab rather than local player. I presume that activating the on/off script on the local player (pressing f) doesn't affect the RemotePlayer Prefab in the same way (even if it has the same script attached).
How do I get the RemotePlayer Prefab flashlight to reflect the same on/off state as the local player? Is this a role of RPC's? I have tried placing a NetworkView on the spotlight with no success. Any guidance would be appreciated.
Answer by syclamoth · Feb 26, 2012 at 03:45 AM
If you have a networkView set to observe a custom script, you need to manually override OnSerializeNetworkView to tell it what information needs to get sent.
If you do so, you need to make sure that both sides of the connection have the same type. While any networkView will connect to every other networkView with the same NetworkViewID, you will have problems with RPCs etc. if both sides don't have the same functions available.
Basically, unless you actually tell the game to send information to the other clients, they simply won't get updated. That is all. If you are using Transform or Rigidbody synchronisation, I would recommend implementing some kind of interpolation or extrapolation instead, so that you don't get jerky remote player movement.
Thanks for the quick response. Again I may misunderstanding how Unity works so excuse me if what I'm about to say is stupid.
Both the RemotePlayer Prefab and the local player are set up with the flashlight & script. The RemotePlayer Prefab is a carbon copy of the local player but with a physical body mesh (rather than a camera). As i understand it the RemotePlayer Prefab is used in the $$anonymous$$ultiplayer game to represent the actions of the local player to all of the other networked players i.e. the local player moving is replicated by the RemotePlayer Prefab so all the other players can see what each player is doing. $$anonymous$$y game transmits all movement data fine - including animations (using $$anonymous$$aximo routines). What I can't get to work is that when the local player command to switch on the flashlight is executed (as I said previously it works as the local player sees the flashlight) the same action on the RemotePlayer Prefab occurs so that everyone else can see it. Do I need to write a script to send the command to the remote player or perhaps have a script on the remote player prefab that watches for the state of the local player flashlight? If so what's the best way of accomplishing this in Unity?
Yes, you need to manually script it either way. It won't automatically watch the state of all public variables- you need to set that up with OnSerializeNetworkView.