How do I replicate my camera rotation?

I want my camera rotation to replicate to the server so the server can tell what the player is looking at.
And I want the clients to know the rotation so that they can see what the player is looking at.

3 Likes

I’m assuming you’re using blueprint.

If your camera is using the control rotation to figure out which direction to look, the server already knows the camera rotation.

First of, to visualize and make debugging easier, open the character blueprint and add a new static mesh component and attach it to the Camera. Set its static mesh to /Engine/EditorMeshes/MatineeCam_SM.MatineeCam_SM (If you don’t see it, tick “Show Engine Folder” under “View Options” in the content browser. If you play test now, the server will see everyone’s camera.

Player controllers exist on the server and on the owning clients. That is, if there’re 5 clients on a listen server, the server will have 6 player controllers (The 5 clients’ and its own), and each client will have 1 (their own).

The control rotation (basically, where the player’s looking) is replicated from clients to the server.
So on the server you can simply get each client’s pawn’s camera rotation no problem (Granted, of course, that your game is using the control rotation to rotate the camera).

To get them to other player’s, you should create a Rotator variable in the character. Eg. call it LookRotation.
Do NOT mark it as replicated. Replicated variables use guaranteed replication which is slow, and a very bad idea to do each frame.

Instead, you make a MulticastSetLookRotation (custom event) with one input (Rotator), and set replicates to “Multicast”. Make sure that “Reliable” is NOT ticked. Make the MulticastSetLookRotation function set the LookRotation to the input, unless executed on the owning client.

http://i.imgur.com/wccAqTu.png


Then in the Tick event of the character, you make a branch with condition of IsServer.
If server, call MulticastSetLookRotation with the value of the “Get Control Rotation” node.

http://i.imgur.com/4ylvM3y.png

This will call MulticastSetLookRotation on all machines (including the server and the owning client).
So after this call, LookRotation has been replicated to all clients, except the owning client (Due to the branch in MulticastSetLookRotation).

Now, regardless of the Branch node in the Tick event, you set the rotation of the CameraBoom to the received LookRotation, if the character does not belong to the local player.

http://i.imgur.com/6VZoqgd.png

Depending on how you do your camera, you may have to alter it a little, but this example should get you started.


http://i.imgur.com/4X9Jyzk.png

11 Likes

100% worked Thanks!
/watch?v=-57LP1UfPl4&feature=youtu.be

I’m glad to help.

1 Like

This help me solve a problem I’ve been having for a while. When a client dies and is respawned in the game, the rotation wasn’t being reset like the host. I also use a “camera boom” but this code didn’t work. Instead of setting the rotation on the spring arm I had to use Player Controller → Set Control Rotation.

So anyway, with that modification this worked for my problem! Thanks!

Thanks works great for me too!

For me, it’s only works with 1st player who is a server. Meaning other players get notified only what 1st player does, but not others. For example if to run a dedicated server, this does not work at all. How to fix this? Thanks in advance.
P.S. I am on 4.12.4

Client to client replication must go through the server.
Create a run on server event and add the replicated variables to it.
From that event create a multicast event and connect the variables through the run on server event.
Then from that set the variables.
Then all variables that you wanted to replicate to all clients will be replicated.

/WTZg0DTl7Cw&feature=youtu.be

Thank you. That works like a charm)

How would I use this in a blenspace?

I love you

thanks MULLEDK solved a long time problem with character look from the server side, seems to be something that should/would work by default but this makes it work.

i know it’s an old question but it’s the only one i have found about it.

Someone know how to set this LookDirection variable to be relative to the front of the camera, cause when i turn my character it doen’t work anymore

This works so amazingly i may just cry.

Still the only working method I can find in 2022 xD
Love you byyyeee!

UE 5.4 It is work!