Search Unity

Hi, im a beginer and i want to:

Discussion in 'Multiplayer' started by Kirby26, Mar 3, 2019.

  1. Kirby26

    Kirby26

    Joined:
    Mar 3, 2019
    Posts:
    13
    Hi. Im making a game. And i need to let the players control gameobjects who are in the scene when the game starts. And sync that gameobject movenent to all the other players. I dont care if is in old or new unet. Y was tryng some tutorials. But all the time all the tutorials move the player, and synchronize the player object or objects spawned by the players.

    This is my actual code. Works in the server, but not in the client. The gameobject "jugador" is the gameobject what i want to move. And i want to let the players to chose what gameobject they want to move using an itputfield. That works too in single, but not online.

    If anyone can help me, thanks.

    Aditionally, anyone know how i can add the "chose a name" in the start network hud?

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.Networking;
    using UnityEngine.UI;

    public class ObjetoJugador : NetworkBehaviour {

    private GameObject Jugador; //Jugador
    public InputField TNombre; //Nombre del Jugador
    private GameObject MainCamera; //Camara
    private Vector3 Distancia; //Distancia de la camara al jugador
    private string Nombre;
    private void Start()
    {
    MainCamera = Camera.main.gameObject;
    }
    private void Update()
    {
    if (isLocalPlayer == true)
    {
    CmdCamara();
    CmdMovimiento();
    }
    }

    [Command]
    public void CmdCamara(){

    Nombre=TNombre.text;

    Jugador = GameObject.Find(Nombre);
    if (Jugador != null)
    {
    MainCamera.transform.position = new Vector3(Jugador.transform.position.x, 50, Jugador.transform.position.z);
    }
    else
    {
    MainCamera.transform.position = new Vector3(this.transform.position.x, 50, this.transform.position.z);
    }

    }
    [Command]
    public void CmdMovimiento()
    { if (Input.GetKeyDown(KeyCode.D))
    {
    Jugador.transform.Translate(5, 0, 0);
    }
    if (Input.GetKeyDown(KeyCode.A))
    {
    Jugador.transform.Translate(-5, 0, 0);
    }
    if (Input.GetKeyDown(KeyCode.W))
    {
    Jugador.transform.Translate(0, 0, 5);
    }
    if (Input.GetKeyDown(KeyCode.S))
    {
    Jugador.transform.Translate(0, 0, -5);
    }
    }
    }
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you're a beginner, don't use Unet. It is deprecated and will be removed from the engine in a matter of months. Also, use Code tags when posting code to the forum so people can read it.

    But to answer your question though, Commands are run on the server, not the client. So none of the code in your Commands will ever run on the client, which includes those Input.GetKeyDown lines. Those are taking the server's input since they are inside a Command. You need to change your code so you take the client's input (so not inside a Command) and then send that input to the server for the server to then move the object.
     
    MrsPiggy likes this.