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 [Command] not working in unity Netcode

Discussion in 'Netcode for GameObjects' started by logicon, Feb 5, 2023.

  1. logicon

    logicon

    Joined:
    Dec 4, 2022
    Posts:
    1
    hello. Im kind of new to unity networking and trying to figure out why i cant get this thing to work.
    this is the script:
    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.Networking;
    3. using Unity.Netcode;
    4.  
    5. public class Player : NetworkBehaviour {
    6.  
    7.     int moveX = 0;
    8.     int moveY = 0;
    9.     float moveSpeed = 0.2f;
    10.  
    11.     void Update () {
    12.         if (!isLocalPlayer) {
    13.             return;
    14.         }
    15.      
    16.         // input handling for local player only
    17.         int oldMoveX = moveX;
    18.         int oldMoveY = moveY;
    19.      
    20.         moveX = 0;
    21.         moveY = 0;
    22.      
    23.         if (Input.GetKey(KeyCode.LeftArrow)) {
    24.             moveX -= 1;
    25.         }
    26.         if (Input.GetKey(KeyCode.RightArrow)) {
    27.             moveX += 1;
    28.         }
    29.         if (Input.GetKey(KeyCode.UpArrow)) {
    30.             moveY += 1;
    31.         }
    32.         if (Input.GetKey(KeyCode.DownArrow)) {
    33.             moveY -= 1;
    34.         }
    35.         if (moveX != oldMoveX || moveY != oldMoveY)    {
    36.             CmdMove(moveX, moveY);
    37.         }
    38.     }
    39.  
    40.  
    41.     [Command]
    42.     public void CmdMove(int x, int y) {
    43.         moveX = x;
    44.         moveY = y;
    45.         isDirty = true;
    46.     }
    47.  
    48.     public void FixedUpdate() {
    49.         if (NetworkServer.active) {
    50.             transform.Translate(moveX * moveSpeed, moveY * moveSpeed, 0);
    51.         }
    52.     }
    53. }
    i copied this from unity scripting api
    the problem is with the [Command] atribute
    upload_2023-2-5_16-3-41.png
    am i missing something?
    note: i only installed unity netcode for game objects and multiplayer tools packages.
     

    Attached Files: