Multiplayer Syncing SetActive Problems

I’m trying to create a multiplayer (UNET) game and in it I’m using the car from the Standard Vehicles Assets, but I’m having trouble disabling the player when he starts controlling the car. When the player tries to get in the car the player is not disabled so the console goes crazy about both the Audio Listeners in the scene (I disable the player and enable the car camera). It also says “Trying to send command for object without authority”. Sorry if I’m not including any useful information, if I am please tell me and I can add it.

It’s supposed to disable the player and allow the car controls to work: Sorry about the resolution

Player and Car prefabs.

My modified version of CarUserControl.cs:

using System;
using UnityEngine;
using UnityEngine.Networking;
using UnityStandardAssets.CrossPlatformInput;

namespace UnityStandardAssets.Vehicles.Car
{
    [RequireComponent(typeof (CarController))]
    public class CarUserControl : NetworkBehaviour
    {
        private CarController m_Car; // the car controller we want to use
        public bool canGetIn = false;
        public bool inCar = false;
        private Vector3 playerEnterOffset;
        private Camera camera;
        public GameObject player;

        private void Awake()
        {
            // get the car controller
            m_Car = GetComponent<CarController>();
            camera = transform.Find("CarCam").GetComponent<Camera>();
            camera.gameObject.SetActive(false);
        }

        [Command]
        private void CmdSetActive (GameObject setObj, bool active)
        {
            RpcSetActive(setObj, active);
        }

        [ClientRpc]
        private void RpcSetActive(GameObject setObj, bool active)
        {
            setObj.SetActive(active);
        }

        private void FixedUpdate()
        {
            //Enter Car
            if (canGetIn && Input.GetKeyDown("q") && !inCar)
            {
                inCar = true;
                CmdSetActive(player, false);
                camera.gameObject.SetActive(true);
                playerEnterOffset = transform.position - player.transform.position;
                print("Entered Car");
                print(playerEnterOffset);
            }
            //Exit Car
            else if (Input.GetKeyDown("q") && inCar)
            {
                inCar = false;
                CmdSetActive(player, true);
                player.transform.position = transform.position - playerEnterOffset;
                camera.gameObject.SetActive(false);
                print("Exited Car");
            }

            if (inCar)
            {
                Movement();   
            }
        }

        private void Movement()
        {
            // pass the input to the car!
            float h = CrossPlatformInputManager.GetAxis("Horizontal");
            float v = CrossPlatformInputManager.GetAxis("Vertical");
            #if !MOBILE_INPUT
                float handbrake = CrossPlatformInputManager.GetAxis("Jump");
                m_Car.CmdMove(h, v, v, handbrake);
            #else
                m_Car.Move(h, v, v, 0f);
            #endif
        }
    }
}

I used a pastebin because this was so big (373 lines). My modified version of CarController.cs: CarController.cs - Pastebin.com

The car also has a script for a trigger collider in one of it’s children:

using UnityEngine;
using UnityStandardAssets.Vehicles.Car;
using System.Collections;

public class GetInCar : MonoBehaviour {

    private CarUserControl userControl;

    void Start()
    {
        userControl = transform.parent.GetComponent<CarUserControl>();
    }

	void OnTriggerEnter (Collider collider)
    {
        //Test
        if (collider.CompareTag("Player"))
        {
            userControl.canGetIn = true;
            if (!userControl.inCar)
            {
                userControl.player = collider.gameObject;
            }
        }
    }

    void OnTriggerExit(Collider collider)
    {
        if (collider.CompareTag("Player"))
        {
            userControl.canGetIn = false;
        }
    }
}

Thanks in advance, fleap

1 Answer

1

fleap3

You can only add the int string bool and float parameters to the [command] prefix. You can not add GameObject.