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.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Input controls both players in multiplayer game (Unity 5)

Discussion in 'Multiplayer' started by curryslurper, Sep 22, 2015.

  1. curryslurper

    curryslurper

    Joined:
    Sep 1, 2015
    Posts:
    1
    Hi

    Newbie here trying to learn Unity so please bear with me. I am trying to create a multiplayer game where each player controls an object. Instead the player is controlling both the objects. Not really sure how to use the 'networkView.isMine' function in 'void Update'. I have found guides online but they are for Unity 3 or Unity 4 but not really sure what to do in Unity 5. Below is the function.

    void Update()
    {
    //InputMovement ();
    NetworkView nView = GetComponent<NetworkView> ();
    if (nView.isMine)
    {
    float moveHorizontal = Input.GetAxis ("Horizontal");
    float moveVertical = Input.GetAxis ("Vertical");

    Vector2 movement = new Vector2 (moveHorizontal, moveVertical);

    rb.AddForce (movement * speed);

    if (Input.GetKeyDown (KeyCode.Space)) {
    Jump ();
    }
    }
    }


    Please help me
     
  2. asperatology

    asperatology

    Joined:
    Mar 10, 2015
    Posts:
    981
    I believed you are playing around with player-owned objects only, is that correct?

    If so, watch the video below:

     
  3. Firoball

    Firoball

    Joined:
    Aug 6, 2015
    Posts:
    60
    NetworkView is old networking and will not work in conjunction with UNET.

    You have to inherit your class from NetworkBehaviour instead of MonoBehaviour and then you can simply use if(isLocalPlayer) {...}
     
    Dreamaster likes this.