isLocalPlayer returns false on remote clients

Hello,

i am building an Minecraft Type clone. It will have mulitplayer networking. Therefore i am using the HLAPI from Unity. This code is still a rough prototype, so it looks messy and most of it is hardcoded. I have the problem, that only the host can spawn objects (cubes) and not the remote clients.

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

/// <summary>
/// Example interactions
/// </summary>
public class Controller : NetworkBehaviour {

    ...... //some properties

    private void Start()
    {
        ....
    }

    private void InitMaterials()
    {
        ....
    }
   
    private void Update()
    {
        Debug.Log("isLocalPlayer=" + isLocalPlayer); //on remote clients this is always false
        if (!isLocalPlayer)
        {
            return;
        }

        //some other stuff....
    }

    /**
     * Example function to demonstrate grid-placing and destroying.
     */
    [Command]
    private void CmdPlacing()
     {
        // place a cube on the grid, roughly where the mouse is positioned
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hitInfo;
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hitInfo))
            {
                CmdPlaceCubeNear(hitInfo.point);
            }
        }

        // destroy an object (hardcoded cube), which the user clicked on
        if (Input.GetMouseButtonDown(1))
        {
            RaycastHit hitInfo;
            ray = Camera.main.ScreenPointToRay(Input.mousePosition);

            if (Physics.Raycast(ray, out hitInfo))
            {
                GameObject clickedObject = hitInfo.collider.gameObject;
                //All Objects can be destroyed, except the plane
                if (clickedObject.tag != "Plane")
                {
                    DestroyCubePrefab(clickedObject);
                }
            }
        }
     }

    /**
     * Places a cube on the grid, near a given point in (world coordinates)
     *
     * @param clickPoint given Point in world coordinates
     */
    [Command]
    private void CmdPlaceCubeNear(Vector3 clickPoint)
    {
        Vector3 finalPosition = grid.GetNearestPointOnGrid(clickPoint);
        GameObject newObject = Instantiate(Resources.Load("Prefabs/Cube") as GameObject, finalPosition, Quaternion.identity);
        NetworkServer.Spawn(newObject);
        this.GetComponent<SaveNLoad>().AddToSceneStorage(newObject);
    }
}

I have a NetworkIdentity (and Network Transform) attached to my Player GO and checked the LocalPlayerAuthority box. And my Player prefab is in the “PlayerPrefab” Box from the NetworkManager (Box “Auto Create Player” is checked).
Can anyone help me, what i did wrong?

You only run the place code on the server seems to be what the issue is.

Hey, thanks for the answer.

How do I make it run on the clients? I mean building as standalone application is enough or do I have to set additional settings?

You use [command] to place the call to ask the server to then run [clientrpc] which should have the code for actually placing the object. You use command as a way to ask the server to then run a clientrpc for you.

Oh okay I will try this.
Do you know why isLocalPlayer returns false on my clients?

Probably because you have LocalPlayerAuthority on, that disassociates the player from the server. It would render the command and clientrpc system useless as well. For command and rpc and also probably isLocalPlayer to work the server needs to be the authority. If you have that checked, the local player becomes the authority over their own session.

That doesn’t work. In Unity when i hover over Local Player Authority it says “True if this object will be controlled by a player on a client” and this is want i want. So I need to check that box.

“Controlled by a player on the client” means the server is not controlling it. When you press a button while playing most modern deterministic online games you are not actually moving the player, spawning an object, or whatever your action is. You are sending a request to the server to move the player for you, the server moves the player and then sends you back the update of where it moved you so that your local client can move to that position. That is what command and clientrpc do.

If you use local player authority, in the scene view while playing the game, you could click on your player and physically move him around and the server would not stop you, because the server is not in authority over the players position. Meaning someone playing your game could use a hack easily to just teleport themselves anywhere they want on the map because the client has authority, the server just says “well, ok, that is where you are now I guess”. They could use speed boosts, teleport, whatever they wanted. If you do not care about that, you can go ahead and check that button because it seems like a quick fix for a single issue. I advise against it though. Checking that box will render your command and clientrpc useless though, and if you are making a minecraft style game, people will just be hacking and doing whatever they want all day long because the server won’t be doing anything about it because it has no authority.

The reason that isLocalPlayer probably is still not working is (if that is all the code you have) that you don’t have a lot of the other fundamental networking aspects in the game yet such as OnServerAddPlayer(), OnPlayerJoined(), NetworkServer.AddPlayerForConnection(conn, player.gameObject, playerID); etc. Unless you have them other places. The reason for sure that only the host can spawn objects is because when you issue a command to the server, the server will do that, but without a clientrpc, the server is not telling the client to do it also. The server will do it on the server side only. You have to execute the clientrpc to make the server tell the player to spawn the object on their end as well.

I advise you look at other already working network examples and read up more before you try to get too far into it incorrectly, it is not fun to have to go back and change things. I have had to do it many times in my travels of Unet networking

https://unity3d.com/learn/tutorials/s/multiplayer-networking

1 Like

Try using Client Network Transform component instead of Network Transform component. Worked for me. Btw I am using Netcode.