Search Unity

Question Are raycasts expensive in multiplayer games?

Discussion in 'Multiplayer' started by AV_Corey, Oct 21, 2022.

  1. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    If I have a MMORPG and detect the user's click position with a raycast (from camera/screen) and then another raycast from the impact point to the ground, will this become expensive/cause issues if say 20 players click at the same time?

    Maybe I'm misunderstanding how game servers work and I'd only need to send the results of the raycast?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    I assume those clicks are for target selection in the world?

    If so, you would have the client send an RPC SelectTargetServerRpc(ulong objectId) and the server may or may not confirm the selection and respond with an appropriate RespondSelectTargetClientRpc(bool accepted).

    The client already has the target selected, but may have to deselect the target and undo any actions performed on that target if the server says "no". Or the client waits for the server response - in an MMO this delay isn't as critical but it may make the UI feel sluggish.

    Is 20 raycasts on the server expensive? No, not at all. But do you want players be able to send raycast request/result messages to the server on every click with users possibly running scripts that do so once every frame? That is a potentially big waste of bandwidth, and that's what can end up being literally expensive!

    I would say based on the question you asked, don't worry too much about what works and what doesn't in an MMO. ;)

    Make a small client-server multiplayer with limited number of players first, and even with 20 players you may find it difficult to get this many players online at the same time. So play it by ear, and scale up as needed, and be prepared to refactor a lot, and don't expect to publish an MMO-style game (or grow the testbed to MMO scale) anytime soon. It'll take several years at least, but don't let that stop you from making a learning experience by setting very reachable targets, and increment on those features.
     
    r31o likes this.
  3. AV_Corey

    AV_Corey

    Joined:
    Jan 7, 2016
    Posts:
    122
    Thanks for your response :)

    So lets say a player casts a raycast every click/frame, and there are 1000 players. That's 1000 raycasts per frame at say, 60 fps which would be 60,000 raycasts/second. This sounds massively expensive so if I ever get to the point of taking my project online I should probably re-write this system, correct?

    On that note, I'm not even beginning to think about putting my single player game online yet and it's still in massively early stages/just a learning project me and my friend are working on. It was just something I've been thinking about but ofcourse wont need to actually worry about until the time comes. (if it ever does) :D
     
  4. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,536
    The Server does not need to do everything a Client does, and vice versa. If you have 1000 concurrent players you'll have multiple servers.
     
  5. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,005
    Yeah, with 1000 connected players you wouldn't worry about raycasts anymore. :)

    This Photon Fusion battleground example advertises 200 concurrent players at 60 fps - which I think may be doable with the best of the best in terms of server hardware and bandwidth - but I still think that's marketing pushing this number. Realistically speaking for a small team with a single server instance and restricted budget you can reasonably aim for at most 64 players in fast-paced games.

    MMOs can do with a little more due to heavily reducing the updates of dynamic information outside of the client's "bubble" ie the client won't get updates of other players/enemies more than a certain distance away. These are the sort of optimizations that will be required when moving to 3-digit number of connected clients.