x


[Closed] 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).

more ▼

asked Jun 04 '12 at 12:41 PM

ExTheSea gravatar image

ExTheSea
2.3k 14 24 32

Is there no one who can debug my code??

Jun 04 '12 at 07:09 PM ExTheSea

The problem is that this can be a lot to read through... I know I wouldn't be able to help you, but there may be someone who can. You might just have to wait longer, because different people are on at different times of the day. Just check in later, and hope for the best. I'm rooting for you!

Jun 04 '12 at 09:02 PM You!

Thanks for that. It just that I'm struggling so long now to get this done and every time I thing I got it...it ends in the player who dismounts spassing out and flying out of the map.

Jun 04 '12 at 09:26 PM ExTheSea

...It's better than nothing. I would have no idea how to get that far...but I may have a small idea to help you. Firstly, you can eliminate the last script from your post, since it has nothing to do with the problem (obviously, since it has to do with weapon switching, which you seem to have down). (This is only to help with readability of your post. If you need help with it, create a new question for it.)

Here's the idea. Mounting and dismounting should be animations (since an action should be an animation). This may seem dicey, but you need to destroy the original player object and the original vehicle, and instantiate a new gameobject for both of them for mounting. Dismounting is reverse; do the animation, and then instantiate the original objects to replace the old, "mounted" one (which will be destroyed).

Does this idea help? I wouldn't be able to really code this, BTW.

Jun 05 '12 at 05:56 AM You!

^^ Thanks You you are a big help. You are giving me the strength to stand for my destiny, stand for my question, for UNITY. Now imagine there would be epic music in the background :).

Jun 06 '12 at 10:30 PM ExTheSea
(comments are locked)
10|3000 characters needed characters left

The question has been closed Jun 12 '12 at 10:24 AM by ExTheSea for the following reason:

Closed because question is to vague and I will maybe find it out myself.


3 answers: sort voted first

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.

more ▼

answered Jun 06 '12 at 10:51 PM

Piflik gravatar image

Piflik
5.4k 17 27 44

First things first: Thank you for your answer as you are the first one :)

2nd: I don't think this is the problem because 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.

Jun 06 '12 at 11:12 PM ExTheSea

@ExTheSea, would you add that info to the question? It surely would be helpful.

Jun 07 '12 at 03:52 PM You!
(comments are locked)
10|3000 characters needed characters left

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

more ▼

answered Jun 12 '12 at 10:00 AM

Bunny83 gravatar image

Bunny83
46.9k 12 50 210

I closed the question and now I will keep trying myself as I'm not that far from solving it. Thx for your answer.

Jun 12 '12 at 10:25 AM ExTheSea
(comments are locked)
10|3000 characters needed characters left

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. :)

more ▼

answered Jun 11 '12 at 09:37 PM

multinfs gravatar image

multinfs
21 9 12 13

In the web demo I uploaded there is a bug I fixed already. When you wanna try it do it like i described in the question. The Client has to check the map which is played on the server before connecting.

Jun 11 '12 at 09:48 PM ExTheSea

I updated it.

Jun 11 '12 at 10:06 PM ExTheSea
(comments are locked)
10|3000 characters needed characters left

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x832
x733
x102
x1
x1

asked: Jun 04 '12 at 12:41 PM

Seen: 1002 times

Last Updated: Jun 12 '12 at 10:45 AM