This works when the host moves the player, and it moves for both host and client, when the client calls this, it is still ran through but the client doesnt move for the host or the client
`
[ServerRpc]
private void MoveServerRPC(int way, int input, PlayerStateMachine player)
{
Vector3 pos;
int steps = DiceManager.Instance.diceNumber.Value;
if (way == 0)
{
pos = new Vector3(player.transform.position.x + steps * input, player.transform.position.y, player.transform.position.z);
}
else
{
pos = new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z + steps * input);
}
player.transform.position = pos;
player.SwitchState(player.spectatingState);
}
Log which “player” it acts upon, mainly the NetworkObjectId.
Also please provide more details about PlayerStateMachine. Is this a NetworkBehaviour? Does it implement INetworkSerializable? I suppose there may be something wrong with that implementation.
Already tried logging the player, seems to act on the correct one which also makes sense since the host doesnt move either when the client tries to.
Here is my statemachine, the states are based on the abstract base state.
public class PlayerStateMachine : NetworkBehaviour
{
public PlayerBaseState rollState = new PlayerRollState();
public PlayerBaseState moveState = new PlayerMoveState();
public PlayerBaseState spectatingState = new PlayerSpectatingState();
public PlayerBaseState currentState;
private void Start()
{
if (!IsOwner)
{
this.enabled = false;
}
currentState = rollState;
}
private void Update()
{
currentState.UpdateState(this);
Debug.Log(currentState);
Debug.Log(IsOwner);
}
public void SwitchState(PlayerBaseState state)
{
currentState.ExitState(this);
currentState = state;
currentState.EnterState(this);
}
}
The game is a board type game so the player first rolls a dice and then moves either direction with steps based on that roll. This all works perfectely with host and is synced to client. From my understanding it should work to just move the transform and let networktransform component sync the gamobjects.
public class PlayerMoveState : PlayerBaseState
{
public override void EnterState(PlayerStateMachine player)
{
}
public override void ExitState(PlayerStateMachine player)
{
}
public override void UpdateState(PlayerStateMachine player)
{
if (Input.GetKeyDown(KeyCode.A))
{
MoveServerRPC(0, -1, player);
}
if (Input.GetKeyDown(KeyCode.D))
{
MoveServerRPC(0, 1, player);
}
if (Input.GetKeyDown(KeyCode.S))
{
MoveServerRPC(1, -1, player);
}
if (Input.GetKeyDown(KeyCode.W))
{
MoveServerRPC(1, 1, player);
}
}
[ServerRpc]
private void MoveServerRPC(int way, int input, PlayerStateMachine player)
{
Vector3 pos;
int steps = DiceManager.Instance.diceNumber.Value;
if (way == 0)
{
pos = new Vector3(player.transform.position.x + steps * input, player.transform.position.y, player.transform.position.z);
}
else
{
pos = new Vector3(player.transform.position.x, player.transform.position.y, player.transform.position.z + steps * input);
}
player.transform.position = pos;
player.SwitchState(player.spectatingState);
}
}
This may or may not fail. Depending on whether the object is in-scene placed or spawned, Start runs either before or after OnNetworkSpawn. Move this code to OnNetworkSpawn. All network properties are considered uninitialized until OnNetworkSpawn ran and after OnNetworkDespawn.
This may be causing your issue.
public override void OnNetworkSpawn()
{
if (!IsOwner)
{
this.enabled = false;
}
currentState = moveState;
}
well i changed it to this and it doesnt seem to work, the owner works as before with the host not having the clients script activataed and vice versa for the client.
Ok so it seems like when i changed the authority mode on network transform to owner it works, even with a serverRPC, why is it on server by deault and is this less safe?