Edit
Okay, so I partially solved the problem by adding a rigid body component to the capsule. I read somewhere that apparently you have to have one in order to move on the server.
Problem 2
The next problem I have is that I can now move a capsule that is spawned by the client fine and the host can move it as well as many times as they like. The problem I am getting now is that when the host spawns a capsule the client cannot move it at all and I get a sort of glitch effect on the client side and the capsule still doesn't move on the host side. Is there a reason why it would only work one way and not the other way around? I thought at first it might have to do with Spawn vs SpawnWithClientAuthority but that doesn't seem to make a difference.
Project Summary
I have a pretty simple multiplayer project, all I want to do is have one player host and the other join as a client. Once they are joined together the two players can spawn a capsule and then when the user clicks on a capsule. They should be able to pick it up and move it around the scene and the other player should be able to see this action. I can get both players to connect to the server and spawn their own capsule and both players can see this. The movement script is done as well. However, it is only transmitted on the host side. When the client picks up the object it does not update on the server.
Problem
I have done a bit of debugging and have found that when I call the command on the client it is not being executed at all through line breaks and simple debug statements.
Code
public void OnInputClicked(InputClickedEventData eventData)
{
Debug.Log("clicked");
if (isLocalPlayer)
{
if (Physics.Raycast(transform.position, direction: transform.forward, hitInfo: out hit, maxDistance: range))
{
Debug.Log("Hit capsule");
objectID = GameObject.Find(hit.transform.name);
objNetId = objectID.GetComponent<NetworkIdentity>();
playerIDThatClicked = player.GetComponent<NetworkIdentity>();
CmdAssignAuthority(objNetId, playerIDThatClicked);
Debug.Log("grabbed");
}
else
{
//if we are releasing the object we want to unassign our authority
if (objNetId != null)
{
CmdAssignAuthority(objNetId, playerIDThatClicked);
objNetId = null;
Debug.Log("released");
}
}
}
}
[Command]
void CmdAssignAuthority(NetworkIdentity target, NetworkIdentity player)
{
Debug.Log("inside cmd");
current = target.clientAuthorityOwner;
playerconn = player.connectionToClient;
if (current != null && current != playerconn)
{
Debug.Log("Had authority now removing and replacing");
target.RemoveClientAuthority(current);
target.AssignClientAuthority(playerconn);
}
else if (current != null)
{
Debug.Log("removing authority");
target.RemoveClientAuthority(current);
}
else if (current == null)
{
Debug.Log("No authority, adding one now");
target.AssignClientAuthority(playerconn);
}
else {
Debug.Log("CmdAuthority error");
target.RemoveClientAuthority(playerconn);
}
}
Question
Am I calling this command correctly? The script is attached to a player prefab and the capsule prefab contains a network id and a network transform. I am pretty new to Unet and this feels like a noob mistake.
NetworkTransform
with the rightTransformSyncMode
\$\endgroup\$objNetId
only set in the place you show in the code above? I have a feeling it isn't giving the correct value on one of the connected machines though I would have to run a test project to really verify the implementation of this function in the complex way you're using above \$\endgroup\$