Search Unity

First Person Controller with Multiplayer

Discussion in 'Multiplayer' started by MitchTJones, Feb 1, 2017.

  1. MitchTJones

    MitchTJones

    Joined:
    Jan 31, 2017
    Posts:
    3
    Hi guys,
    I'm trying to make a simple 1v1 shooter game and I've run into an issue:
    I'm using a Network Manager that spawns a Player Prefab (the included First Person Controller with a capsule)
    I modified the script to turn your character blue with:
    public override void OnStartLocalPlayer()
    {
    GetComponent<MeshRenderer>().material.color = Color.blue;
    }​
    I also added:
    if (!isLocalPlayer)
    {
    GetComponent<FirstPersonController>().enabled = false;
    }​
    to private void start() in order to stop one player controlling both player prefabs.

    Now, connecting one player to the server does nothing wrong and works fine, but once I connect a second player, they are fully functional but the original player (the host) is unable to do anything...

    Any help would be greatly appreciated :)
     
  2. MitchTJones

    MitchTJones

    Joined:
    Jan 31, 2017
    Posts:
    3
    I managed to fix the issue - for anybody experiencing the same problem, add
    GetComponentInChildren<Camera>().enabled = false;​
    into the if (!isLocalPlayer) as well.
     
  3. Trys10Studios

    Trys10Studios

    Joined:
    Jun 24, 2013
    Posts:
    45
    The code above worked wonders for me. I'm doing a simple shooter (first time using Unity's HLAPI and was having some issues with the Unity tutorial). Rendering the camera false seems absolutely necessary in order for the tut script to work. I had followed some other scripts that worked fine (but wanted to start from scratch) and honestly was surprised to have so many issues with the Unity tut. I assumed it was a trick (as some of the other tutorials do) and as I went on it was an unaddressed issue.

    The code I used is as follows and is on the player prefab:

    using UnityEngine;
    using UnityEngine.Networking;

    public class PlayerControllerFPS : NetworkBehaviour

    {
    void Update()

    {
    if (!isLocalPlayer)
    {
    GetComponentInChildren<Camera>().enabled = false;
    return;
    }

    var x = Input.GetAxis("Horizontal") * Time.deltaTime * 150.0f;
    var z = Input.GetAxis("Vertical") * Time.deltaTime * 3.0f;

    transform.Rotate(0, x, 0);
    transform.Translate(0, 0, z);
    }

    public override void OnStartLocalPlayer()
    {
    GetComponent<MeshRenderer>().material.color = Color.blue;
    }
    }
     
    Teknos90 likes this.
  4. tonycentenoa

    tonycentenoa

    Joined:
    Feb 1, 2017
    Posts:
    2
    Thanks! Work good
     
  5. colijn

    colijn

    Joined:
    Aug 1, 2018
    Posts:
    4
    can you post a photo or picture or a copy/paste of your full script?
    every time i try doing it your way it says that the word PRIVATE is wrong
    assuming your where talking about the: first person controller script
     
  6. colijn

    colijn

    Joined:
    Aug 1, 2018
    Posts:
    4

    New
    can you post a photo or picture or a copy/paste of your full script?
    every time i try doing it your way it says that the word PRIVATE is wrong
    assuming your where talking about the: first person controller script
     
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Mitch last logged into the forum over a year ago, so don't expect a fast response.