0

I'm making a mobile game in which there are left/right/jump movement UI buttons. The players should be able to move with those buttons. I'm using Unity and Photon Fusion Host Mode.

The problem is, for the player in client, the movement is very weird. When I press the button, it moves 'a little bit' and then gets back to its original position.

Also, the host is controlling both, the host and client.

Since I'm getting input from the UI buttons, I've not used INetworkInput, OnInput, TryGetPlayerInput or anything else.

I'm just checking this in BeforeUpdate.

public void BeforeUpdate()
    {
        if(Runner.LocalPlayer == Object.InputAuthority)
        {
            isLocalPlayer = true;
        }
    }

And doing the movement in FixedUpdateNetwork

public override void FixedUpdateNetwork()
    {
        if(isLocalPlayer)
        {
            if(moveRight)
            {
                rb.velocity = new Vector2(3,rb.velocity.y);
            }

            if(moveLeft)
            {
                rb.velocity = new Vector2(-3,rb.velocity.y);
            }
        }
    }

How to do player movement using UI buttons using Photon Fusion Host mode?

One more thing, the movement works well when I do the same thing in Shared Mode.

Rahul
  • 3
  • 3

1 Answers1

1

In host mode network input must be used. This is because the client runs the gameplay simulation multiple times per frame and resimulates it as well (client side prediction).

You must:

  1. Create a struct implementing the INetworkInput interface.
  2. Create a MonoBehaviour implementing INetworkRunnerCallbacks and implment code to copy your input state from your buttons into the INetworkStruct. Put this on a GameObject in your scene.
  3. In FixedUpdateNetwork only access Networked properties or input that you get via the GetInput function of the NetworkBehaviour.

Also since you are using a rigidbody make sure that your object has a NetworkRigidbody component and that the physics mode is set to ClientPrediction in the NetworkProjectConfig

Following the Fusion 100 Tutorial will teach you all these concepts as well.

Lukesta
  • 23
  • 3