Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

Question Host & Client user input moving both Player prefabs at the same time

Discussion in 'Netcode for GameObjects' started by Kevin_Buckeejit, Nov 2, 2022.

  1. Kevin_Buckeejit

    Kevin_Buckeejit

    Joined:
    Sep 8, 2022
    Posts:
    8
    I have a player controller attached to the Player prefab - along with the NGO components

    Network Object Client Network Transform Network Animator

    In the Network Manager I have the Player prefab added so that the host & clients will spawn when joining.

    The issue I have is that the user input from client & host is controlling both prefabs on screen at the same time.

    On the player controller script I have the line if (!IsOwner) return; at the start of the Update() before any movement is called

    Any help much appreciated.

    See the attached video for the issue

     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,615
    Post your code too. And some info on the prefab setup.
    Somehow you are applying input to both character controllers, that much is clear.
     
  3. Kevin_Buckeejit

    Kevin_Buckeejit

    Joined:
    Sep 8, 2022
    Posts:
    8
    thanks for the reply!

    The Player Prefab is setup as follows:

    • Player (Has the Network Object & Client Network Transform components)
      • Controller (Has the PlayerController script attached to it)
      • Model (Has the Owner Network Animator component with the Animator applied)
    This is the PlayerController update function:

    Code (CSharp):
    1. void Update()
    2.     {
    3.         if(!IsOwner) return;
    4.      
    5.         if (!rigidbody || !animator) return;
    6.  
    7.         currentVelocity = rigidbody.velocity;
    8.  
    9.         GetAxisHistory();
    10.  
    11.         GetTargetHeading();
    12.  
    13.         GetLocomotionState();
    14.  
    15.         GetTurnSpeed();
    16.  
    17.         if (currentAnimationStates.HasFlag(AnimationPlayerStates.Pivoting))
    18.         {
    19.             SetTurnParams();
    20.         }
    21.         else if (TurnTriggerIsSet())
    22.         {
    23.             SetVelocityToCurrent();
    24.         }
    25.         else
    26.         {
    27.             TurnCheck();
    28.  
    29.             SetLocomotionVelocity();
    30.         }
    31.     }

    **********************************************************************************************
    I am getting these warning messages when the Host & Client Spawn into the scene

    • Model (1) is disabled! Netcode for GameObjects does not support disabled NetworkBehaviours! The OwnerNetworkAnimator component was skipped during ownership assignment!
    • Model (1) is disabled! Netcode for GameObjects does not support spawning disabled NetworkBehaviours! The OwnerNetworkAnimator component was skipped during spawn!

    EDIT:
    I am using the new Input System
     
    Last edited: Nov 2, 2022
  4. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    6,615
    I suppose it could be this but not sure:
    Code (CSharp):
    1. if(!IsOwner) return;
    If you have server-authoritative movement (the default) then the owner might be the server for all player objects. I'm not sure and can't confirm if that holds true for player objects. Try logging NetworkBehaviour.IsOwnedByServer to see if server owns those.

    If you need movement client-authoritative you can get that script from Boss Room example or take mine, it lets you change authoritah at runtime. Sorry about the name, big South Park fan. :)

    Also, you could simply choose to Destroy() or enabled=false the character controller script in OnNetworkSpawn if it isn't the owner object and skip the check in Update. That way you'd also see in the Inspector who got the controller and who doesn't and you don't have to add even more IsOwner checks anywhere (ie OnDisable, LateUpdate, and so on).
     
  5. Kevin_Buckeejit

    Kevin_Buckeejit

    Joined:
    Sep 8, 2022
    Posts:
    8
    So I have the soloution working in another smaller projects using

    Code (CSharp):
    1. public override void OnNetworkSpawn()
    2.     {
    3.         if (!IsLocalPlayer) enabled = false;
    4.     }
    This is turning off the player controller script for the player that is not local.
    This works fine in the prototype project but I cant get it to work the same way in my main project
    The script is disabling as expected but the 2 players are still being controlled on both the Host & Client from both inputs.

    In the editor everything is showing as expected for both players (IsHost / IsLocalPlayer / Script disabled)

    Could this be an issue with the new input system? Do I need to have a control scheme for Player1 & Player2?
     
  6. TestifyNow

    TestifyNow

    Joined:
    Feb 9, 2016
    Posts:
    24
    New input system is fine, and you don't need control scheme for player1 & player2. That wouldn't make any sense unless you're trying to do split-screen co-op game.
     
  7. Kevin_Buckeejit

    Kevin_Buckeejit

    Joined:
    Sep 8, 2022
    Posts:
    8
    So it was because I was using 2 instances on the same machine - the input was being read by both. I was able to get the outcome when I played a host & client across 2 different machines using gamepads. Each gamepad controlled their own player. Problem solved!
     
    barisdincer likes this.