- Home /
get in get out of a car. Unet
I am making multiplayer fps shooter, and it works perfectly, but when it comes to vehicles i got some problems, cause i am kinda new to Unet. I wrote script for getting in the vehicle and it works normally, for singleplayer and i have no idea how to implement this into multiplayer, i tried these "ClientRpc" stuff, (didnt work), "Command" and etc. so basically Player has Controll script (which i use for moving him) and this script has function OnControllerColliderHit, so if this happens i call void Disable(); here is script anyways: `
void OnControllerColliderHit(ControllerColliderHit hit)
{
//so if we hit car
if(hit.collider.tag == "Vehicle")
{
//if we pressed "E"
if (Input.GetKeyDown(KeyCode.E))
{
//call method to disable some stuff taht should be disabled, like: controller, shoot, weapons etc.
disable(inVehicle, hit);
}
}
}
//method to disable some stuff taht should be disabled.
void disable(bool inVehicle, ControllerColliderHit hit)
{
//Vehicle has script PlayerControll (not for moving player, just stupid name), where it controlls how many players are in car, is there driver or not, etc.
hit.gameObject.GetComponent<PlayerControll>().CmdSit(transform.name); //yeah, i am trying [Command] thing here.
inVehicle = true;
//character cntroller is disabled;
cc.enabled = !inVehicle;
//if it is not localplayer, so for other players: my colldiers should be disabled....
if (!isLocalPlayer)
{
for (int i = 0; i < disableForRemote.Length; i++)
{
disableForRemote[i].SetActive(!inVehicle);
}
}
// as well as mine for me lol.
else
{
for (int i = 0; i < disableForClient.Length; i++)
{
disableForClient[i].SetActive(!inVehicle);
}
}
//disabling scriptsssssss....
for(int i = 0; i< disableComponentForClientAndRemote.Length; i++)
{
disableComponentForClientAndRemote[i].enabled = false;
}
}
(pls read them it's not big, just comments)
and My "PlayerControll script (that is NOT for controlling/moving player, just name...)"
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class PlayerControll : NetworkBehaviour {
//max players in vehicle. (lol why 1?? :DDD)
public int MaxPlayers = 1;
//cammera
public Camera cam;
//Sync var so every player can know how many of players are in the car.
[SyncVar]
public int sitPlayers;
//Sync var so every player can know does this car have driver or not(none of them is not working BTW. as well as network transform. )
[SyncVar]
public bool hasDriver = false;
//this is where i "teleport" player that became driver of car. and i make player child of Driver GO.
public GameObject Driver;
//same here but for passanger.
public GameObject Sitter;
void Start()
{
cam.enabled = false;
cam.GetComponent<AudioListener>().enabled = false;
GetComponent<AudioSource>().Play();
}
void Update()
{
//if there is driver in car then we can controll it. (it's a paradox for me, if there is driver and i am passanger i also can controll it lol.)
if(hasDriver)
{
GetComponent<carController>().enabled = true;
}
else
{
GetComponent<carController>().enabled = false;
}
}
//command function for sitting. in car.
[Command]
public void CmdSit(string _player)
{
//i increase how many people are in car
sitPlayers++;
cam.enabled = true;
//find player who sat there.
GameObject player = GameObject.Find(_player);
//i think you will get it >>
if (hasDriver)
{
player.transform.parent = Sitter.transform;
player.transform.position = Sitter.transform.position;
cam.GetComponent<AudioListener>().enabled = true;
}
else
{
player.transform.parent = Driver.transform;
hasDriver = true;
cam.GetComponent<AudioListener>().enabled = true;
player.transform.position = Driver.transform.position;
}
}
//"Unet is easy" they said....
void Out()
{
//havent made i yeeeet.
}
}
First of all... thanks for reading this..
and thanks for any help/ comment/downvote/upvote
--Nick.
Your answer
Follow this Question
Related Questions
Why does my car re-align after drift into the original direction? 0 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Car Audio c# script not working 3 Answers
Variables not sync'd to clients 0 Answers