Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.

Second player unable to move in Photon Fusion

Discussion in 'Multiplayer' started by oppggmm, Mar 28, 2023.

  1. oppggmm

    oppggmm

    Joined:
    May 2, 2022
    Posts:
    1
    I am using Photon Fusion for my multiplayer game and I am having an issue where the second player who joins the game is unable to move. The first player is able to move without any problems. Both players are able to join the game and spawn correctly, but the second player cannot move their character. I have checked that both players have control of their respective objects and have also checked that the inputs are being sent correctly. Is there something specific that needs to be done in Photon Fusion to enable movement for the second player? Any help or suggestions would be greatly appreciated.

    PlayerMovement script:

    Code (CSharp):
    1. using Fusion;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4. using UnityEngine;
    5. using UnityEngine.UIElements;
    6.  
    7. public class PlayerMovement : NetworkBehaviour
    8. {
    9.     [SerializeField] float moveSpeed = 4f;
    10.     [HideInInspector] public Joystick joystick;
    11.     public float turnspeed;
    12.     [Networked] Vector3 right { get; set; }
    13.     [Networked] Vector3 forward { get; set; }
    14.     private Vector3 playerPosition;
    15.  
    16.     public override void Spawned()
    17.     {
    18.    
    19.         //get instance from Playerinput script 2 value.
    20.  
    21.  
    22.     }
    23.     public override void FixedUpdateNetwork()
    24.     {
    25.    
    26.  
    27.         if (Object.HasStateAuthority == false) return;// have to check at this.
    28.  
    29.  
    30.         if (Runner.TryGetInputForPlayer<PlayerInput>(Object.InputAuthority, out var input)) // this out keyword will find PlayerInput script and assign the value all the information from that script and put it into input variable.
    31.         {
    32.             Debug.Log("joystick is moving" + input.HorizontalInput  +  input.VerticalInput);
    33.  
    34.             turn_Player(input);
    35.             if(input.HorizontalInput !=0 &&input.VerticalInput!=0)
    36.             {
    37.                 Move(input);
    38.  
    39.             }
    40.         }
    41.     }
    42.  
    43.     //Don't for get to send the joystick value across the network.
    44.     // parameter list
    45.     public  void Move(PlayerInput input)
    46.     {
    47.         right = Camera.main.transform.right;
    48.         right = new Vector3(right.x, 0f, right.z);
    49.         right = Vector3.Normalize(right);
    50.          forward = Camera.main.transform.forward;
    51.         forward = new Vector3(forward.x, 0f, forward.z);
    52.         forward = Vector3.Normalize(forward);
    53.  
    54.  
    55.         Vector3 rightMovement = right * moveSpeed * Runner.DeltaTime * input.HorizontalInput; //Runner.DeltaTime same as Time.deltatime that make your movement moving smoother
    56.         Vector3 upMovement = forward * moveSpeed * Runner.DeltaTime * input.VerticalInput;
    57.         Vector3 heading = Vector3.Normalize(rightMovement + upMovement);
    58.         transform.position += heading * moveSpeed * Runner.DeltaTime;
    59.    
    60.     }
    61.     // parameter list
    62.  
    63.     public void turn_Player(PlayerInput input)
    64.     {
    65.         Vector3 direction = new Vector3(input.HorizontalInput, 0, input.VerticalInput);
    66.         if (direction != Vector3.zero)
    67.         {
    68.             Quaternion lookRotation = Quaternion.LookRotation(direction);
    69.             transform.rotation = Quaternion.RotateTowards(transform.rotation, lookRotation, turnspeed * Runner.DeltaTime);
    70.         }
    71.     }
    72.  
    73. }
     
    Last edited: Mar 28, 2023
  2. ramonsmelo

    ramonsmelo

    Joined:
    Jul 31, 2017
    Posts:
    37
    Hi,

    I would like to suggest you follow the Fusion 101 Tutorial series, it will give you a general idea of how the prediction on the clients works.
    Mainly the Step 3: https://doc.photonengine.com/fusion/current/fusion-100/fusion-103

    If you properly assign the Input Authority of your Network Object to your second player, then it should work just fine, so double-check it.

    Looking at your script, there are a few points:

    1. There is no need to check for the State Authority, the FixedUpdateNetwork will be invoked only on the State Auth and the Input Auth (for prediction purposes).
    2. You can simplify your code by only invoking `GetInput` (which will return true only if there is an Input for that Tick for this NO).

    --
    Ramon Melo
    Photon Fusion Team