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.

Server sided damage control

Discussion in 'Multiplayer' started by SadisticLotus, Oct 4, 2022.

  1. SadisticLotus


    Oct 8, 2019
    Hey everyone,

    Im not even sure how to type out my question or how to search for likewise questions so here we go.
    I want to make sure the server calculates the damage of a player so i can validate it. I know most of the times to counter latency(or at least fake it) it is run on the client and the server will validate it. However how would this work if you have for example crit chance cause if i understand correctly the client could have a value that does apply the crit chance and if the same code is run on the server it could leave it out.

    How would you make sure the exact same calculation is done so it can be validated?

    Base damage 20, Crit chance is 30%, crit damage is 7
    Players hits, applies base damage crit chance goes through adds 7 damage

    Player hit function is triggerd
    20 damage crit chance fails damage stays 20

    Hope this makes sense!
  2. r31o


    Jul 29, 2021
    Damage isnt calculated like that.

    When you, for example shoot a bullet on a fps game, you send a rpc to the server saying "hey, I want to shoot!". Then the server checks if the shoot cooldown has passed, or if you have enough ammo. Then, the server itself shoots a raycast, and damage the player. Thats the simplest way.

    Some games simulate the shoot localy to display effects, but never damage the player.

    And, finally, lag compensation:
    -Imagine that you have 300 ping, and you shoot a bullet with the crosshair exactly centered on the enemy, you shoot, you see the blood or whatever effect the game has, but the enemy still there. Why? Because of LAG (I hate that word :mad:). When you sent the rpc, the enemy WAS on the center, but when the the server receives the rpc, the player has moved, so it isnt there at the moment of simulating.

    -What multiplayer games do is lag compensation or collider rollback:
    -You send the rpc and the exact current time to the server
    -The server puts all the hitboxes at the position they where when th rpc was sent.
    -The server shoots a raycast.
    -The server puts the hitboxes back at the current position
    -Everyone is happy :)

    So, that how damage is calculated.
    SadisticLotus likes this.
  3. Punfish


    Dec 7, 2014
    If you want to provide real-time results on client while waiting on server validation you will want to use a set seed for your critical hit calculations. Client and server use the same seed, so they both score crits at the same time.
    SadisticLotus likes this.
  4. SadisticLotus


    Oct 8, 2019
    Thanks @Punfish and @r31o !
    I'm setting the random seed when a player joins and save the state per player.
    The client calculates the damage with his values and so does the server with the stored values, and if they match they are valid!

    Thanks for the help!
    Punfish likes this.