- Home /
Closed because question is to vague and I will maybe find it out myself.
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).
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!
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.
...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 eli$$anonymous$$ate 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. $$anonymous$$ounting 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.
^^ 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 :).
Answer by Piflik · Jun 06, 2012 at 10:51 PM
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.
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.
@ExTheSea, would you add that info to the question? It surely would be helpful.
Answer by multinfs · Jun 11, 2012 at 09:37 PM
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. :)
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.
Answer by Bunny83 · Jun 12, 2012 at 10:00 AM
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
I closed the question and now I will keep trying myself as I'm not that far from solving it. Thx for your answer.