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.

Resolved Can´t move client, netcode for gameobjects.

Discussion in 'Netcode for GameObjects' started by motillaones, Nov 28, 2022.

  1. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    17
    Hii, i'm investigating netcode for gameobjects and doing a test I can't move the client player. When I connect to the server, the "EmptyPrefab" only has the component networkobject, in order to spawn one player or another (classes etc), I have done it with this code;

    public override void OnNetworkSpawn()
    {
    base.OnNetworkSpawn();

    clientID = (int)OwnerClientId;

    if (IsOwner)
    {
    player = SpawnPlayer();
    }

    }

    // Update is called once per frame
    public GameObject SpawnPlayer()
    {
    GameObject[] spawnPoints = GameObject.FindGameObjectsWithTag("SpawnPoint");

    GameObject go = Instantiate(player, spawnPoints[Random.Range(0, spawnPoints.Length - 1)].transform.position, Quaternion.identity);
    go.GetComponent<NetworkObject>().SpawnWithOwnership(OwnerClientId);

    return go;
    }

    In the spawned player i did just this only to test :

    private void Update()
    {

    GetComponentInChildren<Camera>().enabled = true;
    GetComponentInChildren<AudioListener>().enabled = true;
    GetComponent<FirstPersonController>().enabled = true;
    GetComponent<CharacterController>().enabled = true;
    }

    I added the ClientTransformNetwork and nothing happens, i can't even move the character in the client itself, does anyone know what might be happening? Ty.
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    1,909
    Any error messages in the Console window?
    SpawnPlayer must be run on the server. The IsOwner check seems inadequate if this script runs on an object that is spawned for every client since you cannot spawn on clients.
    Please add more context since its impossible to tell which script on which object each of these methods run on. And use code tags.
     
  3. motillaones

    motillaones

    Joined:
    Oct 11, 2018
    Posts:
    17
    I was able to fix it by changing CanCommitToTransform (On NetworkTransform) to false and when spawning the player change CommitToTransform to true if IsOwner.

    "The IsOwner check seems inadequate if this script runs on an object that is spawned for every client since you cannot spawn on clients." Yes, I have changed it to if (isServer).

    I don't know if it will be the best solution of all but at least now it works. Thank you :)