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.

Question How to properly use Lerp or Slerp to change rotation of a network transform

Discussion in 'Netcode for GameObjects' started by brakarb, Dec 1, 2022.

  1. brakarb

    brakarb

    Joined:
    Jul 20, 2018
    Posts:
    2
    Hello,

    im relatively new to unity and even more so to netcode for gameobjects, so its possible that Im missing something simple.
    My issue is with updating the rotation of a network transform using a ServerRpc and the huge difference in how long the rotation takes between Host and Client.

    When doing it like this the object of corse rotates immediately on the Host (which is not what I want) and on the Client it actually looks ok:

    Code (CSharp):
    1. currentRotation = targetRotation;
    2. UpdateRotationServerRpc(currentRotation );
    When using Quaternion.Slerp or Lerp it takes extremly long on the Client (and also looks "jittery") but is looking very good on the Host:

    Code (CSharp):
    1. currentRotation = Quaternion.Lerp(transform.rotation, targetRotation, Time.fixedDeltaTime * _rotationSpeed);
    2. UpdateRotationServerRpc(currentRotation);
    The ServerRpc is as simple as this:
    Code (CSharp):
    1. private void UpdateRotationServerRpc(Quaternion targetRotation, ServerRpcParams serverRpcParams = default)
    2.     {
    3.         Logger.Instance.info($"Executing UpdateRotationServerRpc for Client {serverRpcParams.Receive.SenderClientId} now");
    4.         transform.rotation = targetRotation;
    5.     }

    Any hints on what im missing and/or doing wrong here would be appreciated.

    Kind regards
    Markus
     
    Last edited: Dec 2, 2022
  2. Punfish

    Punfish

    Joined:
    Dec 7, 2014
    Posts:
    304
    Why are you not using the built in components?
     
  3. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    355
    Hi @brakarb , if you use the built-in NetworkTransform (or the ClientNetworkTransform if you want clients to manage movement), it will handle the position/rotation synchronization for you. Would that solve your problem?
     
  4. brakarb

    brakarb

    Joined:
    Jul 20, 2018
    Posts:
    2
    thanks for your answers guys, appreciated.

    I am using NetworkTransforms and position and rotation are synced, thats not the issue. The issue is the HUGE difference in rotation speed between Host (fast/expected speed) and Client(MUCH slower and "jittery" rotation) when using Quaternion.Slerp or Lerp and then updating the rotation using ServerRpc.

    I dont want clients to be able to change their position or rotation directly, so this should be Server Authoritive only.
     
  5. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    355
    I'm afraid that what you're looking for is an implementation of Rollback or Prediction + Reconciliation, which is something that NGO does not provide out of the box right now. You can understand more about them here and here.
    I hope this helps!
     
  6. AdowTatep

    AdowTatep

    Joined:
    Feb 5, 2015
    Posts:
    6
    I'm doing a third person shooter style prototype with netcode for gameobjects. And when using
    NetworkTransform
    , it has a very noticeable movement lag on the client, even though the documentation says it handles interpolation. Does that mean I need to have client side prediction instead? To have instant responsive client inputs?

    I tried looking at the BossRush example but it's very complex to find what you want, not being used to the architecture, and it uses a point-and-click, not a wasd based movement

    Attached my example code
     

    Attached Files:

  7. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    355
    Hi @AdowTatep , yes. The only way to have instant movement on the local player is to process the input locally (client side prediction).
    If you wait until the server processes that, like in your script, your movement will be delayed by the RTT (Return trip time = ping * 2) between you and the server.
    This is why techniques like prediction + reconciliation exist, but their implementation is not trivial and can vary a lot depending on the project...
     
  8. AdowTatep

    AdowTatep

    Joined:
    Feb 5, 2015
    Posts:
    6
    So why use network for game objects if I can't even control the character on the clients without some lag
     
  9. RikuTheFuffs-U

    RikuTheFuffs-U

    Unity Technologies

    Joined:
    Feb 20, 2020
    Posts:
    355
    Let me clarify: you need Client-Side Prediction for instant Server authoritative (=non-cheateable) movement.

    You can also run objects as client authoritative (= cheateable) with instant movement.

    This is not something specific of netcode for gameobjects: it's an underlying issue of multiplayer games.