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

Multiplayer Inputs

Discussion in 'Multiplayer' started by Flynn, Jun 24, 2009.

  1. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    I have been wondering about this for a while: How do I have an object, only respond from the input of a specific player - not all the others. To truly explain this, I will give a scenario:


    two players join a server - when they join, an empty gameobject is instantiated for each player.



    player one's computer ran the code to instantiate gameobject1.
    player two's computer ran the code to instantiate gameobject2.

    Each gameobject has a script containing a single boolean variable.

    The boolean variable is set to toggle when the up key is pressed - in this case I am using the built in inputs system (not really necessary, but ideal):


    Code (csharp):
    1. var upArrowPressed = false;
    2. function Update ()
    3. {
    4. if (Input.GetButtonDown ("input"))
    5. {
    6. upArrowPressed = !upArrowPressed;
    7. }
    8. }

    Now, using my current knowledge, no matter which player clicked the up button, both gameobjects' booleans are toggled - in other words:

    if player one presses up, both gameobject 1, and two's booleans are toggled.
    if player two presses up - the same thing happens.

    I am trying to get object one to only respond to input from the computer that ran the code to instantiate it; In other words, I want gameobject one to only respond to player one pressing the up arrow - and vice versa for player, and gameobject two.


    How might I do that?

    Thank you for the help!

    P.S.: My words can be rather hard to understand, if I have not conveyed my point, please tell me, and I will try to rephrase things
     
  2. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    Best solution is to enable/disable scripts according to use. If you make a script only to observe controlls then there is no reason for it to use resources (calling Update()) on remote players and can just as well be disabled (.enable = false, preventing update from being called)

    A not so pretty solution can be if you have a networkview attached (usually have since its multiplayer) then you can see if the owner of the networkview is you (networkview.isMine).

    I really do recommend the first option though.

    Hope it helps.
    //perlohmann
     
  3. Veli

    Veli

    Joined:
    Mar 10, 2009
    Posts:
    301
    I'd recommend the 1st option as well, since the netwrokview.isMine doesnt work in TPS environments.
     
  4. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    I am not sure I quite understand.... Is there any way to get the input of the computer that instantiated stored in a variable? (e.g., if the up arrow is pressed, a boolean variable is marked true)


    Also, I am using network views. Does the first option still work?
     
  5. perlohmann

    perlohmann

    Joined:
    Feb 12, 2009
    Posts:
    221
    I have no idea on your GameObject setup or how you instantiate your objects across the network.
    A good guess at something that would work would be to put the following code (in java though) in the start function in the class that has your input control.

    Code (csharp):
    1.  
    2. //its C# but guess the difference aint that big =)
    3. void Start()
    4. {
    5.    if (!gameObject.networkView.isMine)
    6.    {
    7.       //this is the essential of it. set enabled to false will prevent the Update function from being called.
    8.       this.enabled = false;
    9.    }
    10. }
    11.  
    Yes
     
  6. Flynn

    Flynn

    Joined:
    May 24, 2009
    Posts:
    311
    Than - OH!!!!! I was about to ask something but I get it now!!!! There is no network view concentrating on the script (commonly), so you can disable all other scripts without the other players experiencing any difference!


    Instead of having your script ignore all other computers, have all other scripts ignore your computer!



    Now had I thought of that...!


    Thanks for the help!


    One last question - in javascript, does "this.enabled = false" ALWAYS disable Update(), or does it disable the function that it is placed within? Thank you!


    (I can work with it just being update, but it is ideal to have as many options open as possible)


    EDIT:

    Whoops! I just remembered - all components (other than gameobject, and transform) have an "enabled" parameter - it disables the whole script!