Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question Client transform updating slow

Discussion in 'Netcode for GameObjects' started by angeloneldios, May 1, 2022.

  1. angeloneldios

    angeloneldios

    Joined:
    Jul 7, 2019
    Posts:
    1
    So i don't understand why but the clients are moving like half of the host speed. In my code i change the transform every update with Time.deltaTime multiplier. if anyone can give me a reason why this is happening or if i am doing something wrong i would appreciate it.
    Its a grid based 2d game so you select a grid with your click and the player moves there based on a vectorlist.
    Video:

    Here is the code:


    void Update()
    {
    ClientInput();
    transform.position = playerPosition.Value;

    }

    void ClientInput()
    {
    if (!isMyTurn.Value || !IsLocalPlayer) return;


    if (state.Value == State.Moving) Move();

    if (Input.GetMouseButtonDown(0) && state.Value == State.Normal)
    {
    SetStateServerRpc(State.Moving);
    SetTargetPositionTo(Utils.GetMouseWorldPosition());

    }
    }

    public void SetTargetPositionTo(Vector3 targetPosition)
    {
    currentPathIndex = 0;

    pathVectorList.Clear();
    pathVectorList = Pathfinding.Instance.FindPath(transform.position, targetPosition);

    if (pathVectorList != null && pathVectorList.Count > 1)
    {
    pathVectorList.RemoveAt(0);
    //OnPositionReachedServerRpc();
    }
    else
    {
    //notReachableServerRpc();
    SetStateServerRpc(State.Normal);
    Debug.Log("path vector list is lower than 1");
    }
    }

    private void Move()
    {
    if (pathVectorList != null && pathVectorList.Count > 0)
    {
    Vector3 targetPosition = pathVectorList[currentPathIndex];
    if (Vector3.Distance(playerPosition.Value, targetPosition) > 0.05f)
    {
    Vector3 moveDir = (targetPosition - playerPosition.Value).normalized;

    SumbitPositionServerRpc(playerPosition.Value + moveDir * speed * Time.deltaTime);
    if (Vector3.Distance(playerPosition.Value, targetPosition) < 0.05f)
    {
    SumbitPositionServerRpc(targetPosition);
    }
    }
    else
    {
    currentPathIndex++;
    if (currentPathIndex >= pathVectorList.Count)
    {
    pathVectorList.Clear();
    SetStateServerRpc(State.Normal);
    EndTurnServerRpc();
    //onPositionReached?.Invoke();
    }
    }

    }
    else
    {
    //notReachable?.Invoke();
    Debug.Log("Not rechable");
    SetStateServerRpc(State.Normal);
    }
    }

    [ServerRpc]
    void SumbitPositionServerRpc(Vector3 position)
    {
    playerPosition.Value = position;
    }