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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Simple Movement

Discussion in 'Netcode for GameObjects' started by T3M4CH, Jan 18, 2022.

  1. T3M4CH

    T3M4CH

    Joined:
    Aug 29, 2019
    Posts:
    5
    Can't understand. Please help. No moving, I try to send directly to the client but don't know how. And can't understand how to move player via rigidbody.
    Code (CSharp):
    1. void Update()
    2.     {
    3.         if (IsLocalPlayer)
    4.         {
    5.             #region Keyboard'n Joystick Control
    6.             if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
    7.             {
    8.                 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    9.                 transform.localRotation = Quaternion.Euler(0, Mathf.Rad2Deg * Mathf.Atan2(direction.x, direction.z), 0);
    10.             }
    11.             else if (joystick.Direction != Vector2.zero)
    12.             {
    13.                 direction = new Vector3(joystick.Direction.x, 0, joystick.Direction.y);
    14.                 transform.localRotation = Quaternion.Euler(0, Mathf.Rad2Deg * Mathf.Atan2(direction.x, direction.z), 0);
    15.             }
    16.             else
    17.             {
    18.                 direction = Vector3.zero;
    19.                 animator.SetFloat("Speed", 0);
    20.             }
    21.             Direction.Value = direction;
    22.             animator.SetFloat("Speed", Mathf.Max(Mathf.Abs(direction.x), Mathf.Abs(direction.z)));
    23.             //rigidBody.velocity = Vector3.ClampMagnitude(direction * speed, speed);
    24.             //UpdateDirectionClientRpc(direction);
    25.         }
    26.         #endregion
    27.         if (IsHost)
    28.         {
    29.             rigidBody.velocity = Vector3.ClampMagnitude(Direction.Value * Speed.Value, Speed.Value);
    30.         }
    31.     }
    32.     [ServerRpc]
    33.     void UpdateDirectionClientRpc(Vector3 direction)
    34.     {
    35.         Direction.Value = direction;
    36.     }
     
  2. Kyperr

    Kyperr

    Joined:
    Jul 15, 2016
    Posts:
    32
    It looks like you have the ServerRpc call commented out. That was a little closer to the correct answer.
    That being said, just for the sake of getting somewhere, the code you supplied should work for host at least.
    Does it work on host?

    Perhaps there is a separate issue. Do you have the rigidbody configured correct? Make sure you don't have any of the position checkboxes checked that is locking it in place. You could also temporarily remove the rigidbody and move it via direct transform.position changes to troubleshoot.
     
  3. T3M4CH

    T3M4CH

    Joined:
    Aug 29, 2019
    Posts:
    5
    That's work. But not for Velocity don't know why. Speed greater than 0. Rigidbody network attached to GO
    Code (CSharp):
    1. void Update()
    2.     {
    3.             #region Keyboard'n Joystick Control
    4.         if (IsClient)
    5.         {
    6.             if (Input.GetButton("Horizontal") || Input.GetButton("Vertical"))
    7.             {
    8.                 direction = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical"));
    9.                 transform.localRotation = Quaternion.Euler(0, Mathf.Rad2Deg * Mathf.Atan2(direction.x, direction.z), 0);
    10.             }
    11.             else if (joystick.Direction != Vector2.zero)
    12.             {
    13.                 direction = new Vector3(joystick.Direction.x, 0, joystick.Direction.y);
    14.                 transform.localRotation = Quaternion.Euler(0, Mathf.Rad2Deg * Mathf.Atan2(direction.x, direction.z), 0);
    15.             }
    16.             else
    17.             {
    18.                 direction = Vector3.zero;
    19.                 animator.SetFloat("Speed", 0);
    20.             }
    21.             animator.SetFloat("Speed", Mathf.Max(Mathf.Abs(direction.x), Mathf.Abs(direction.z)));
    22.             UpdateDirectionServerRpc(direction);
    23.         }
    24.         #endregion
    25.         if (IsServer)
    26.         {
    27.             transform.position += direction * Time.deltaTime;
    28.            // rigidBody.velocity = direction * speed;
    29.         }
    30.     }
    31.     [ServerRpc(RequireOwnership = false)]
    32.     public void UpdateDirectionServerRpc(Vector3 direction)
    33.     {
    34.         this.direction = direction;
    35.     }
     
  4. Kyperr

    Kyperr

    Joined:
    Jul 15, 2016
    Posts:
    32
    I'd add some Debug.Log() statements to check to make sure its a sane value.

    I also don't see people using rigidbody.velocity very often. I'd assume that would work, but perhaps its being overwritten by something.
    Try using rigidbody.AddForce()