Entering and Leavinging a Car/Tank/... in an FPS

Closed

Hey guys.

I’m currently making a Multiplayer FPS and I thought about implementing driveable Vehicles. So I made some scripts and they work when there is just one player on a server. But immediatly after another one connects to it it starts bugging out.

The bug is that when you dismount the vehicle the player is stuck in the vehicles-graphic and all of the weapons are equipped. Also the vehicle and the player flies out of the map while spassing out.

The current bug is described in the edit at the end of the question.

Like I said everything works totally fine when there is just one player on the server.

Another thing is that when a player gets into the vehicle and moves around with it the other players don’t see it moving and the player does not disappear and stands still.

I hope the demo makes my problem more clear.

Here are the scripts I used:

var InVehicle: boolean=false;
var Vehicle: GameObject;
var cam : Transform;
function Start () {
if(!networkView.isMine)
enabled=false;

cam=Camera.main.transform;
}


           ...//The following part gets executed when you press the Use-Key and a raycast hits a car in a predefined range

		InVehicle=true;
		Vehicle= hit.collider.gameObject; //gets the gameobject of the vehicle
		
		Vehicle.GetComponent(VehicleMovementScript).enabled=true; // Gets the Movementscript of the vehicle which is also in this question
		if(networkView.isMine)
			Vehicle.Find("DriverCam").GetComponent(Camera).enabled=true;
		var VehicleNV: NetworkView = Vehicle.GetComponent(NetworkView);
		var PlayerObjectNV : NetworkView = PlayerObject.GetComponent(NetworkView);
		VehicleNV.RPC("GetPlayer", RPCMode.AllBuffered, PlayerObjectNV.viewID); //sends the player's viewid to the vehicle
		PlayerObject.SetActiveRecursively(false); // Deactivates the Player
		
		...

@RPC
function Dismount() // gets called by the Vehicle when pressed the Use-Key while mounted
{
InVehicle=false;
if(Vehicle!=null){

		Vehicle.GetComponent(VehicleMovementScript).enabled=false;
		if(networkView.isMine)
		Vehicle.Find("DriverCam").GetComponent(Camera).enabled=false;
		
Vehicle=null;
}
}

This is the script attached to the player. Its called “VehiclePlayer”.

Here is the script “VehicleMovementScript” attached to the vehicle. Its just a cube with a rigidbody and to empty gameobjects. One for the driver and one for the playerDismount.

var Driver : GameObject;
var mounted:boolean=false;

function Start () {
if(!networkView.isMine&&!Driver.networkView.isMine)
enabled=false;
}



function Update () {

//Here would be the part that lets the car move

if(Input.GetButtonDown("Use")&&mounted&&networkView.isMine){
Driver.SetActiveRecursively(true);
mounted=false;
Driver.transform.position=transform.Find("DriverDismount").transform.position;

Driver.Find("Main Camera").Find("Weapons").GetComponent(NetworkView).RPC("Dismount",RPCMode.AllBuffered);
Driver.Find("Main Camera").Find("Weapons").GetComponent(NetworkView).RPC("Reselect",RPCMode.AllBuffered);
}
}

@RPC
function GetPlayer(PlayerObj:NetworkViewID){
var DriverNV=NetworkView.Find(PlayerObj);
Driver= DriverNV.observed.gameObject.gameObject;
mounted=true;
}

This script calls a function called “Reselect” which is attached to the same gameobject as “VehiclePlayer”. This function should select the weapons which are picked up by the player.

Here is the info You recommended I add to the question. It’s what I answered to the first answer:

“1. I changed the position the driver dismount various times and 2. It works when only one player is connected, the server. It just starts bugging out when the 2nd player is connected.”

I hope someone can help me and debug my code because I couldn’t figure it out by myself until this point.

EDIT:

I made some efforts:

I had a line in my update which sets the player at the position of the vehicledriver-Object. I deleted this line and now after leaving the “car” the player gets dismounted like I want him to. The only problem is that the Reselect function gets called at the wrong player. I mean:

Player A gets in Car 1 and dismounts, Reselect wasn’t called at player A. Now if player B gets in Car 2 (because car 1 is not mountable at this point[not intended])…and dismounts the Reselect function gets called at player A not at Player B. The same thing happens when Player A then mounts and dismounts Car 1 (which then is mountable again. Car 2 isn’t).

That’s just a wild guess, but if you’re using physics and reactivate the player too close to the vehicle when you dismount, he might be pushed away ridiculously fast. Be sure to keep the player outside of the vehicles collider.

Well, I can’t help you with this since I’m not that into this and are’nt familiar with networking but when I checked out your demo I noticed that the 2nd player cant connect, I tried with the default port to make a server and with another that i know is open and still nothing, i tried selecting the map too but it gets stuck when it says starting game.

Just thought I’d help. :slight_smile:

This isn’t going to end anytime soon… You provided just some snippets. Network related stuff can be very sensitive. Always keep in mind what happens on what peer.

For example it doesn’t make much sense to access Camera.main in a multiplayer game. Camera.main always returns the first camera that is tagged “Main Camera”. If you spawn 2 or more players this won’t get you the “correct” camera.

There are also some strange things that doesn’t make much sense. You enable the VehicleMovementScript on every object but the vehicle’s camera only for the player that owns the viewID?

Vehicle.GetComponent(VehicleMovementScript).enabled = true;
if(networkView.isMine)
    Vehicle.Find("DriverCam").GetComponent(Camera).enabled = true;

You should do a full evaluation of your code. Make yourself clear what code is executed in which case and when it should be executed. Insert Gebug.Logs that print the players name / viewId at many places in your code to see what get executed by whom and on which pc.

I don’t think we get anywhere with that question. It’s not really a clear and sepcific question. If you need personal help, post the question on the forums. UnityAnswers is for clear questions that can be answered.

Read the FAQs to see where you should post what