Search Unity

Multiplayer using object in scene

Discussion in 'Multiplayer' started by intonator, Sep 7, 2017.

  1. intonator

    intonator

    Joined:
    Sep 7, 2017
    Posts:
    1
    Hi everybody,

    I am getting in touch with unity and play around with the standard assets. The goal is to create a multiplayer racing game. The player spawns as "Ethan" and can use cars parked around the terrain.

    I am using all the standard assets: Ethan as Person, MultipurposeCameraRig, car, NetworkManager, etc.
    Everything is working fine but I have problems letting the spawned player use a car already placed in the scene. I managed to do it in singleplayer but it doesn't work in multi.
    My idea was to have a boxcollider on the car as trigger. The OnTriggerEnter does:
    - change tags (Ethan = Untagged, car = Player)
    - disable PlayerControlscript of Ethan
    - enable CarControlscript of car
    - change Target of Camera to car
    - up to this point it works fine in singleplayer
    - "put the car into the network" -> fails: car can not be controlled by player and many errors:
    "NullReferenceException: Object reference not set to an instance of an object
    UnityStandardAssets.Vehicles.Car.CarController.SteerHelper () (at Assets/Standard Assets/Vehicles/Car/Scripts/CarController.cs:273)"

    it refers to that line of code:
    m_Rigidbody.velocity = velRotation * m_Rigidbody.velocity;

    It seems that my network-player-instance doesn't "know" about the rigidbody and cant't control it.

    Maybe the simple question should be asked: How can I set a car already placed in the scene as network-player and let the player have control of it?

    Following code is used:
    Code (CSharp):
    1. void OnTriggerEnter (Collider other)
    2.     {
    3.         if(other.gameObject.name == "PlayerPerson(Clone)")
    4.         {
    5.             NetworkIdentity oldPlayer = GameObject.FindGameObjectWithTag ("Player").GetComponent<NetworkIdentity> ();
    6.             GameObject myCar = this.transform.root.gameObject;
    7.             GameObject myPerson = other.gameObject;
    8.  
    9.             other.gameObject.tag = "Untagged";
    10.             myCar.tag = "Player";
    11.  
    12.             GameObject myCameraRig = GameObject.Find ("MultipurposeCameraRig");
    13.             AutoCam ac = myCameraRig.GetComponent<AutoCam>();
    14.             ac.SetTarget (this.transform);
    15.  
    16.             CarUserControl carScript = myCar.GetComponent<CarUserControl> ();
    17.             carScript.enabled = true;
    18.  
    19.             ThirdPersonUserControl PersonScript = other.gameObject.GetComponent<ThirdPersonUserControl> ();
    20.             PersonScript.enabled = false;
    21.             other.gameObject.SetActive (false);
    22.  
    23.             NetworkIdentity netident = myCar.GetComponent<NetworkIdentity> ();
    24.  
    25.             NetworkConnection conn = oldPlayer.connectionToClient;
    26.             NetworkServer.ReplacePlayerForConnection(conn, myCar, 0);
    27.         }
    28.  
    29.     }
    I hope that someone is willing to help. Thanks in advance.

    intonator