- Home /
Forces only working for host [Multiplayer]
Hello. I am new to programming and am making a simple game where you can move a cube around by using forces. How it works is that when you press W, A, S, or D, it adds a force to a cube that moves it in it's appropriate direction. Simple. I am experimenting with adding online multiplayer using UNet, but I'm having a problem. While the host can move their cube just fine, the client can't move the cube at all. It seems to be a problem with the Forces themselves. To test this, I wrote a very simple script.
using UnityEngine;
using UnityEngine.Networking;
public class Test : NetworkBehaviour
{
public Rigidbody rb;
// Update is called once per frame
void Update()
{
if (isLocalPlayer)
{
rb.AddForce(10, 0, 0);
}
}
}
Trying it out, when the Host starts the game, the cube is immediately launched into the distance. But when a client connects and the second cube spawns in, it doesn't move at all. Even though they are using the same code. Please help me with this. Thank you.
Answer by olejuer · Jul 04, 2020 at 08:56 AM
Apparently, isLocalPlayer is false in the second case. This is given by how you spawn the cube. Is it the player prefab or do you instantiate it with some script? I have not been working with UNet for a while, but are there not flags to give authority to the client or keep it for the server? I mean in the inspector of the NetBehaviour script. Multiplayer is always hard. Your approach with a simple test script is a very good idea.