Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

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:
    18
    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:
    6,503
    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:
    18
    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 :)
     
  4. progCan

    progCan

    Joined:
    Apr 27, 2021
    Posts:
    7
    use client network transform instead of just network transform