Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Server authoritative rigidbody movement?

Discussion in 'Netcode for GameObjects' started by TheRareStudios, Aug 15, 2023.

  1. TheRareStudios

    TheRareStudios

    Joined:
    Dec 25, 2021
    Posts:
    14
    Hello I am having trouble with learning Netcode for Gameobjects. But I am working on a 2D space shooter multiplayer game.
    My problems is that my player movement is based on a rigidbody and I can’t find anything about server authoritative rigidbody movement.

    Could someone please help me?
     
  2. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    69
    Add a NetworkObject, NetworkTransform and NetworkRigidbody to your object. It will automatically get set to kinematic on the clients so it only reacts to server-side physics and the server will be authoritative over its movement. Only scripts running on the server will be allowed to move it and when it does it will automatically sync to all clients connected.

    If you want clients to move their characters, you will want to use a ClientNetworkTransform instead. You'll also encounter all the different issues with lag that occur depending on whether the server or client owns a specific object, experiment with it a bit to learn about how all that works.
     
  3. TheRareStudios

    TheRareStudios

    Joined:
    Dec 25, 2021
    Posts:
    14
    But would it be possible to solve it with RPCs that the players only send the inputs to the server and the server will move the player.

    Would that be possible?
     
  4. Nyphur

    Nyphur

    Joined:
    Jan 29, 2016
    Posts:
    69
    Yes, but everyone except the host would experience significant lag when moving their character and movement would feel unpredictable as there would be lag delays both on pressing and letting go of keys. Games instead tend to make movement happen instantly on the local machine to make the game feel responsive and then that movement or the new position is sent to the server and synced to all other players.

    This opens the game up to cheating because the client has authority over its own position, but there are ways to solve that. The server can sense-check the moment so that it detects a person moving faster than should be possible, for example. There are other strategies, like you could make the client see their movement happen immediately but in the background their inputs get sent to the server like you suggest and their real movement happens there -- then the client just needs to check if its own position is very far off the server's position at any point, and re-sync with the server.

    The short answer is that no matter the strategy used, you want the person playing to see and feel like their inputs are moving the character instantly and without lag.
     
    CodeNinja- likes this.
  5. TheRareStudios

    TheRareStudios

    Joined:
    Dec 25, 2021
    Posts:
    14
    Ok thanks