- Home /
Script for getting in and out of a tank
Hi there, I am making an FPS game and I need to add tanks and helicopters. But the thing is, i don't know how to write a script that would allow me to enter a vehicle and get out of a vehicle when pressing the button "e" (E = Enter). Can you please tell me what to do and post a script please. I have been searching on the internet for 2 hours now and everything related that i find, i don't understand it or it isn't what i needed.
Answer by aldonaletto · May 05, 2012 at 07:15 PM
Since effectively entering the tank is a complex job, a simpler solution is to deactivate the original character and activate a new one inside the vehicle - the vehicle itself is actually a specialized character, whose hierarchy should be something like this:
Tank <- "empty object" with rigidbody and collider matching the tank dimensions TankModel <- tank model Main Camera <- the tank camera
The tank script must take care of the FPS character / tank driver switching, as well as the other movement and firing controls:
var player: Transform; // drag the player here var speed: float = 5.0; // tank moving speed var turnSpeed: float = 30; // tank turning speed in degrees/second var enterRange: float = 8.0; // distance to enter the tank var exitPos = Vector3(0, 1.5, 0); // exit position relative to the tank
private var cam: Camera; // tank camera private var inTank = false; // tells when the character is inside the tank
function Start(){ cam = GetComponentInChildren(Camera); cam.enabled = false; // make sure the tank camera is off }
function EnterTank(){ // enters the tank: player.gameObject.SetActiveRecursively(false); // deactivate the original player... cam.enabled = true; // and activate the tank camera... inTank = true; // and controls }
function ExitTank(){ // move the player to its exiting position: player.position = transform.TransformPoint(exitPos); player.gameObject.SetActiveRecursively(true); // reactivate the original player... cam.enabled = false; // and deactivate the tank camera... inTank = false; // and the tank controls }
function Update(){ if (inTank == false){ // if outside the tank... // and E pressed while player inside the enter range: if (Input.GetKeyDown("e") && Vector3.Distance(player.position, transform.position) turnSpeed Time.deltaTime, 0); var vel = transform.forward Input.GetAxis("Vertical") speed; rigidbody.velocity = Vector3(vel.x, rigidbody.velocity.y, vel.z); // place here other control code, like turret rotation, // cannon elevation, firing etc. } } }
Answer by Piflik · May 05, 2012 at 06:09 PM
If you come across a problem, just think about what has to happen. In this case:
check if the player is looking at a tank/helicopter and is close enough
hide the tank/helicopter model
put the camera on the tank's/helicopter's position/rotation
change the control scheme from standard FPS to tank/helicopter
Putting this in code-form might not be trivial, but also not too hard, methinks.
Your answer
Follow this Question
Related Questions
Prefabs & scripts 1 Answer
Automatically uncheck? 1 Answer
How to optimize this script and add items imediately without grids 0 Answers
DecoratorDrawer javascript examples 0 Answers
How to save data on iOS? 1 Answer