I try to implement multiplayer shooting using raycasts. Usually when the player press the shoot button i cast a ray from the center of the camera in the current direction. But now i am confused how to do that in a multiplayer environment.

My setup: There is a single camera in the scene, when a player joins he owns that one locally. When a player shoots i cast a ray in the local instance and tell the host the result using a "Command" function that applies damage to the corresponding player. Respawn in my case means reset health and position to a "networkstartposition". Now i realized when a player gets shot and dies/respawn, there is a little lag and if the player who shot him has a fast gun, he will apply damage even if he is already dead because in his local instance the position has not yet been updated correctly. Its only 1 or 2 shots but its too much.

So i changed it to when a player shoot, he does not cast a ray instead calls a "Command" function to cast a ray on the host but realized that on the host the remote players has no camera.

Now i am stuck, if i attach a camera to every player prefab and activate them at startup only for the local player then i also cant use it because they are deactivated on the host.

My current attempt is to just pass position and lookdirection to the "Command" function but get the same results.

So how should i proceed here? I also tried different QoS settings with the same result.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.