Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink
Go down
Member [Lv. 3]
Member [Lv. 3]
Posts : 16
Points : 1517
Rep : 4
Join date : 2014-07-11
Age : 19
Location : usa
View user profilehttp://truefpscombat.wix.com/fpsassalt

Need an enter exit vehicle script please help

on Tue Sep 02, 2014 2:53 pm
I want to make it so i spawn a my fps player but if i walk up to my tank or jet ect and press E the player no longer moves and i can start driving the tank around and when im done i press E and it gets me out.Thank you
Posts : 194
Points : 1992
Rep : 44
Join date : 2013-12-30
View user profile

Re: Need an enter exit vehicle script please help

on Wed Sep 03, 2014 4:49 am
This should have been posted in the Help(General) section of the forum.

--New Developer
Posts : 608
Points : 3168
Rep : 406
Join date : 2013-12-11
Age : 21
View user profilehttps://www.youtube.com/user/misterninjaboy

Re: Need an enter exit vehicle script please help

on Wed Sep 03, 2014 10:52 pm
This system is not as simple as you may think.. its not one set of scripts and method to have a vehicle enter/ exit system, that functions in a way to enable vehicle mode and the default, so requesting someone to make you this whole system for free is a bit much.. Im sure you can find some good recources on how to do this, or better yet, you can find assets on the asset store.. But the main outline for this system is as follows:

Player enter Car trigger => Player learns of car's existence
Player hit a key and a car is known => function enterCar
Play animation?
Destroy Player / Disable Renderer / Hide under terrain
Give control to car > Enable Script / Set a boolean variable
Move around
Player hit a key = > function exitCar
Take control off car
Play animation
Instantiate Player back at the left of the car
Give control back to player
Member [Lv. 3]
Member [Lv. 3]
Posts : 45
Points : 1396
Rep : 3
Join date : 2015-08-30
Age : 23
Location : Melbourne
View user profilehttp://jakeaquilina.weebly.com/

Re: Need an enter exit vehicle script please help

on Fri Sep 18, 2015 11:55 pm
ok you can do this very easily.

a few things you will reguire

add a trigger to the car depending on how you want it, generally near the drivers door.
add an empty GameObject to the car for where you will spawn when you leave the vehicle, generally at drivers door.
we also will use a boolean because we are making a toggle script

i use the examplle : bool inVehicle;

add a script to the car as the car will determine weather you are near it or not

add this function


OnTriggerStay(Collider col)
if(col.tag == "Player" && Input.GetbuttonDown(KeyCode.E) && inVehicle == false)
//we trigger the inVehicle bool to true
inVehicle = true;

//or may need to use
col.enabled = false;
//find which one of the 2 above will work.

//now we need to enable this vehicles controller scripts.

gameObject.getcomponenet<MyControlelerScript>().enabled = true;

//if the above does not work use SetActive(true), if neither work

//now we need to do the same thing as above in reverse to get out of the vehicle, but we dont care about the trigger anymore because our new player is the vehicle.


  if(Input.getKeyDown(KeyCode.E) && inVehicle == true)


//first while the player is disabled, we need to move his pos to our vehicles Spawn Point, we will enable the player last.

/*if this does not work it might be because the player is disabled, might have to do the moving whe nwe enable the player just do it one line after we enable the player so they dont see the glitch move. but i dont think this will be a problem. */

GameObject.Find("Player").transform.position = SpawnPoint.position;

//set the inVehicle bool to false
inVehicle = false;

 //GetComponenet on the vehicle controls and camera also if you need to and disable them.

//now we need to enable this vehicles controller scripts.

gameObject.getcomponenet<MyControlelerScript>().enabled = false;

//if the above does not work use SetActive(true)

//now we can enable the player to control.

//or if that does not work

GameObject.Find("Player").enabled = true;



this is a guide to how to do it, most of the code should work fine but might need some tweaking, this is not very difficult to do.

only things you really need to do is add a player spawn poinjt to each vehicle, where you want them to spawn when they leave the vehicle, and secondly you need a way to interact with the vehicle, generally by a trigger on each vehicle. and all you need now is to talk to both player and vehicle to enable/Disable correct components and you are done. oh and also when you leave the vehicle just need to pop the player to your vehicles spawn point pos which you can do by referencing a public Transform or GameObject on each vehicle.

hope this explains it all out for you =)
Back to top
Permissions in this forum:
You cannot reply to topics in this forum