Search Unity

Question Rotation of the networkobject with interpolation. Netcode

Discussion in 'Netcode for GameObjects' started by Donkrokodil, Mar 22, 2024.

  1. Donkrokodil

    Donkrokodil

    Joined:
    Aug 16, 2019
    Posts:
    9
    I am using Netcode for Gameobjects. There is one problem: suppose that I want to rotate the one instantiated networkobject (Unity2D) from 0 to 90 angle instantly (networkobject with networkrigidbody2D,clientnetworktransform with interpolation enabled). Code is quite simple within Update:/
    Code (CSharp):
    1.  if (!IsOwner) { return; }
    2. if (Input.GetKeyDown(KeyCode.W))
    3. {
    4.      NetworkObject.transform.localRotation = Quaternion.Euler(0, 0, 90f);
    5. }
    6. if (Input.GetKeyDown(KeyCode.S))
    7. {
    8.      NetworkObject.transform.localRotation = Quaternion.Euler(0, 0, 270f);
    9. }
    10. if (Input.GetKeyDown(KeyCode.D))
    11. {
    12.      NetworkObject.transform.localRotation = Quaternion.Euler(0, 0, 0f);
    13. }
    14. if (Input.GetKeyDown(KeyCode.A))
    15. {
    16.      NetworkObject.transform.localRotation = Quaternion.Euler(0, 0, 180f);
    17.  
    18. }
    In this case, when the client rotates the networkobject, it rotates instantly. However, when the host rotates its object, it doesn't rotate immediately (it rotates gradually during less than 0.5 seconds) from the point of view of another client.
    When I turn off interpolation in the ClientNetworkTransform, rotation is instant everywhere, but movement of the NetworkObject includes obvious lags. So, how should I rotate the object instantly with interpolation enabled?
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,025
    This should work without interpolation:
    Code (CSharp):
    1. GetComponent<NetworkTransform>().Teleport(transform.localPosition, yourRotation, transform.localScale);
    Depending on whether you update with local or world coords you may have to omit the "local" and supply the world position.