Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Problem with networkView.isMine

Discussion in 'Multiplayer' started by marf, Jul 20, 2011.

  1. marf

    marf

    Joined:
    Jul 20, 2011
    Posts:
    124
    Hi, i'm making an online fps and i've some problems with moving only my pg.

    I use this code :

    Code (csharp):
    1. if(networkView.isMine){
    2.  
    3. // Move scripts
    4.  
    5. }

    When i start the server the play that is created moves correctly, but when i connect another player, this player can't move and when it try to move, it moves the first player created, the same think happen to the first player, when i try to move it only the second moves.

    Moviments are reversed after that the second player is created!!


    I don't think that the code is wrong...

    What i have to do?
     
    Last edited: Jul 20, 2011
  2. marf

    marf

    Joined:
    Jul 20, 2011
    Posts:
    124
    Anyone know what is the problem?
     
  3. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    Hard to tell without code, can you post it so we can have a look ?
    Can you also tell us how you instantiate your players ?
     
  4. marf

    marf

    Joined:
    Jul 20, 2011
    Posts:
    124
    I istantiate the players with Network.Instantiate The code is an if that include the code for the movements only if the network view is mine...
     
  5. dlcl

    dlcl

    Joined:
    Oct 15, 2010
    Posts:
    33
    Hi marf - I can't 100% say for sure that this is the solution as we don't know what your movement scripting consists of, but I solved this problem for my game by disabling the Camera component of any instantiated Player that doesn't belong to the controlling Player on Awake()

    In the script that handles my Player movements I have this:

    Code (csharp):
    1.  
    2. public void Awake()
    3. {
    4.         if (!networkView.isMine)
    5.         {
    6.          GetComponentInChildren<Camera>().enabled = false; // disable the camera of the non-owned Player;
    7.  
    8.          GetComponentInChildren<AudioListener>().enabled = false; // Disables AudioListener of non-owned Player - prevents multiple AudioListeners from being present in scene.
    9.          }
    10. }
    11.  
     
  6. appels

    appels

    Joined:
    Jun 25, 2010
    Posts:
    2,687
    marf, when do you instantiate your player, in what function ? OnClientConnected or OnConnectedToServer ?
     
  7. marf

    marf

    Joined:
    Jul 20, 2011
    Posts:
    124
    I instantiate thev player in function OnConnectedToServer() and function OnServerInitialized() so, when i client connect or the server isinstantiated.

    This is spawn code:

    Code (csharp):
    1. var playerPrefab : Transform;
    2.  
    3. function OnConnectedToServer()
    4. {
    5.     Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
    6. }
    7.  
    8.  
    9. function OnServerInitialized()
    10. {
    11.     Network.Instantiate(playerPrefab, transform.position, transform.rotation, 0);
    12. }
    13.  
    14. function OnPlayerDisconnected (player : NetworkPlayer)
    15. {
    16.     Debug.Log("Server destroying player");
    17.     Network.RemoveRPCs(player, 0);
    18.     Network.DestroyPlayerObjects(player);
    19. }
     
  8. marf

    marf

    Joined:
    Jul 20, 2011
    Posts:
    124
    @dlcl I attached this in the inspector, but nothing happens...
     
    Last edited: Jul 21, 2011
  9. marf

    marf

    Joined:
    Jul 20, 2011
    Posts:
    124
    No one?
     
  10. marf

    marf

    Joined:
    Jul 20, 2011
    Posts:
    124
    Last edited: Jul 25, 2011
  11. ZioBorghi

    ZioBorghi

    Joined:
    Jul 25, 2011
    Posts:
    1
    I have the same problem..
     
  12. boco

    boco

    Joined:
    Dec 7, 2010
    Posts:
    373
    It sounds like a Camera issue are you creating cameras with the player prefab? if so then you need to turn off the camera then enable if

    pseudo code

    function start
    if networkView.isMine()
    cam.enabled = true;
    else
    cam.enabled = false;

    this should fix any camera issue you have because I had the same problem in my game back in the day.. well a year ago check it out though:

    My video : http://www.youtube.com/watch?v=Ayn9BcoKVIY
     
  13. priceap

    priceap

    Joined:
    Apr 18, 2009
    Posts:
    274
    you are describing a problem in your script for player control - where you need to have the client control his locally network.instantiated player and any players that connect and create a remotely network.instantiated player is controlled remotely.

    Your reply with code to appels about where/when you are doing the instantiating seems ok (although I do it in a OnNetworkLevelLoaded function as in the Unity networking example project.)

    NetworkView.isMine only returns true if the object was instantiated locally, so that would seem to suggest more than one player is being locally instantiated and therefore input control moving to that one, but otherwise there does not seem to be enough to go on in what you have provided to take a guess.

    The third-person multiplayer example in Unity's networking example project is about as bare bones of an an example as you can get, and the network.instantiating and player control using NetworkView.isMine all work as expected. You might try drilling through that project and find where you might be missing something.