- Home /
Enter ehicle code help
I'm working on some code that allows a player to get in and out of a vehicle, but I've come across a problem. This is the code to get in:
void OnTriggerStay(Collider col){
if (col.CompareTag ("Cokpit")) {
Debug.Log ("I can get in a plane");
if(Input.GetKey(KeyCode.E)){
Debug.Log ("I'm in a plane");
camController.cam1.camera.enabled = false;
camController.cam2.camera.enabled = true;
aeroPlaneControls.playerInPlane = true;
//InVehicle = true;
InVehicle = !InVehicle;
capCol.enabled = false;
}
}
}
and this is to get out:
void update {
if (InVehicle && Input.GetKey(KeyCode.E)) {
Debug.Log ("Leaving the Plane");
Vector3 setup = transform.position;
setup = vhclExitPoints[0].transform.position;
transform.position = setup;
//InVehicle = false;
InVehicle = !InVehicle;
camController.cam1.camera.enabled = true;
camController.cam2.camera.enabled = false;
aeroPlaneControls.playerInPlane = false;
capCol.enabled = true;
}
}
When I press 'E' it jumps between in the vehicle and out of the vehicle. What do I need to do to make it so the player can just press 'E' to get in, then 'E' again to get out?
Answer by Jeff-Kesselman · May 05, 2014 at 06:25 PM
Combine the entry and exit code in a single component on the player. Switch which you do based on InVehicle
Answer by NoseKills · May 05, 2014 at 08:31 PM
Use Input.GetKeyDown("e") instead of GetKey("e")
GetKey("e") returns true every time in the update method as long as you hold down the key. That's why you get swapped in and out repeatedly on consequent Update() calls.
GetKeyDown("e") returns true only on the Update() call when the key was first pressed down.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Flip over an object (smooth transition) 3 Answers
Boat movement, driving and animation 1 Answer