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

Resolved Player prefab not move in clients only host.

Discussion in 'Netcode for GameObjects' started by rogelioae, Apr 25, 2023.

  1. rogelioae

    rogelioae

    Joined:
    May 20, 2022
    Posts:
    19
    I'm trying to move a player on a client and it only moves on the host. I did the essentials, I added a NetworkManager, in the field player prefab I added my prefab player, the prefab has its Network Object and its NetworkTransform, and my code is the following:

    [RequireComponent(typeof(InputController))]
    public class SoldadoController : NetworkBehaviour
    {
    [SerializeField] private float velocidadDeMovimiento;
    private CharacterController player;
    private Animator animator;
    private InputController _inputController = null;

    private void Awake()
    {
    _inputController = GetComponent<InputController>();
    }
    void Start()
    {
    player = GetComponent<CharacterController>();
    animator = GetComponent<Animator>();
    }

    void Update()
    {
    Vector2 input = _inputController.MoveInput();

    if(IsServer && IsLocalPlayer){
    Debug.Log("Server");
    Move(input);
    }else if(IsClient && IsLocalPlayer){
    Debug.Log("Server Rpc");
    MoveServerRpc(input);
    }
    }

    private void Move(Vector2 input){
    player.Move( transform.right * input.x * velocidadDeMovimiento * Time.deltaTime);


    player.Move( transform.forward * input.y * velocidadDeMovimiento * Time.deltaTime);

    }

    [ServerRpc]
    private void MoveServerRpc(Vector2 _input){
    Debug.Log(_input);
    Move(_input);

    }

    Sorry i dont know put code
     
  2. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    175
    Hi.
    Did you add "network transform" component to player object?
    Edit: (I saw that you said added)
    I really highly recommend you video of CodeMonkey's Tutorial.
     
  3. rogelioae

    rogelioae

    Joined:
    May 20, 2022
    Posts:
    19
    Yes Mj-kkaya I have add a networktranform.
     
    Mj-Kkaya likes this.
  4. Mj-Kkaya

    Mj-Kkaya

    Joined:
    Oct 10, 2017
    Posts:
    175
    I would like to make a suggestion about your code.
    "Vector2 input = _inputController.MoveInput();" Do not put the line at the top of update.
    Put these inside of if/else statement.

    For example, if you have 5 players on the stage.
    For the other 4 players, the Vector2 input = _inputController.MoveInput();" line would be run unnecessarily.

    Vector2 input = _inputController.MoveInput();

    Code (CSharp):
    1. void Update()
    2. {
    3.    if(IsServer && IsLocalPlayer)
    4.    {
    5.       Move( _inputController.MoveInput());
    6.    }
    7.    else if(IsClient && IsLocalPlayer)
    8.    {
    9.       MoveServerRpc( _inputController.MoveInput());
    10.    }
    11. }
     
  5. rogelioae

    rogelioae

    Joined:
    May 20, 2022
    Posts:
    19
  6. CodeNinja-

    CodeNinja-

    Unity Technologies

    Joined:
    Feb 22, 2023
    Posts:
    27
    Hi rogelioae,

    I guess one solution could have been to disable the PlayerInput component on the player prefab and enabling it in the OnNetworkSpawn override if you're the owner:
    Code (CSharp):
    1. public override void OnNetworkSpawn()
    2. {
    3.     if (!IsOwner) return;
    4.     GetComponent<PlayerInput>().enabled = true;
    5. }
    6.  
    Was that your solution?

    FYI: To put code in your post you have to click on the button Insert Code, it will open a popup where you can select the language and paste your code, here is a screenshot:
    upload_2023-4-28_17-20-58.png
     
    Mj-Kkaya likes this.
  7. rogelioae

    rogelioae

    Joined:
    May 20, 2022
    Posts:
    19
    thank for the code, i dont know as put it.