Search Unity

Question Forces only working for Host

Discussion in 'Multiplayer' started by RarePika, Jul 4, 2020.

  1. RarePika

    RarePika

    Joined:
    Nov 16, 2016
    Posts:
    4
    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.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    The way you have your code written, you are trying to move the object on the client if it is the client's Player GameObject. Doing it this way you'd need to make sure you are using client authority for the GameObject. If you're using server authority you might see it move just on the single client where isLocalPlayer is true, but otherwise the server is just going to send position updates to tell all clients where it thinks the object is (not moving). This is assuming you actually have the NetworkTransform component attached.
     
    RarePika likes this.
  3. RarePika

    RarePika

    Joined:
    Nov 16, 2016
    Posts:
    4
    That worked! Thank you!
     
    Joe-Censored likes this.