- Home /
Enter/Exit Vehicle with the same key?
Hello guys,
My player can not enter and exit a vehicle with a single key:
OnTriggerStay(){
if(Input.GetKeyDown(KeyCode.F)){
enterVehicle();
Debug.Log("Vehicle: " + interactableObject + " Entered");
}
}
When the player is in the vehicle, the script looks again (activates following code) if the key is pressed, but immediately. Since one presses the key more than 1 frame (so the key is not released), this causes the player to exit the vehicle after he got in. LOL
Update(){
if(Input.GetKeyDown(KeyCode.F)){
exitVehicle();
Debug.Log("Jumped off Vehicle: " + interactableObject);
}
}
I wonder if there were a function called Input.SetKeyUp() to reset the state of the key / change it back to false. If so, I would write it underneath the enterVehicle()-command in the if-statement to prevent getting off the car in the second part.
However, this works if I ask for different keys as expected.
Get$$anonymous$$eyDown "It will not return true until the user has released the key and pressed it again." link text
The problem maybe in enterVehicle(); method
Try to make a bool
if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.F)){
incar = !incar;
}
if(incar){
enterVehicle();
}else{
exitVehicle();
}
thanks, but in the else part I control the playermovement, which needs to run every frame, so exitVehicle() runs also every frame.
Answer by Hexer · Jul 20, 2015 at 07:53 PM
Instead of putting the command for exiting the vehicle in the update function. You can make it a function of its own (for the sake of this post we will call that function (void ExitVehicle). Thus OnTriggerStay() if the keycode F is pressed. InvokeRepeat the function : void ExitVehicle() Put this InvokeRepeating on your OnTriggerStay()
//0.1f is when the function gets called after it execute that line of code.
//60.0f is, how many times it repeats the function. (because this is 60 it is the same as the (void Update)
InvokeRepeating("ExitVehicle",0.1f,60.0f);
void ExitVehicle(){
if(Input.GetKeyDown(KeyCode.F)){
exitVehicle();
//cancles the invoke called ExitVehicle
CancelInvoke("ExitVehicle")
Debug.Log("Jumped off Vehicle: " + interactableObject);
}
}
You also maybe want to assign some parameters to your OnTriggerStay. That will prevent weird glitches like entering a vehicle while you are miles away standing in another collider.
EDIT : and as Positive7 mentioned. you can also make a bool. (I overlooked the fact that the movementscript has to be On all the time. So you can make a bool like this.
bool VehicleEnter = false;
void OnTriggerStay(){
If(Input.GetKeyDown(KeyCode.F)&& !VehicleEnter){
VehicleEnter = true;
enterVehicle();
}
}
void Update(){
if(Input.GetKeyDown(KeyCode.F) && VehicleEnter){
VehicleEnter = false;
exitVehicle();
}
}
I am not sure if this code will work because I haven't tested it but if !VehicleEnter and VehicleEnter doesnt work in the ifstatement, than change them to VehicleEnter==false or VehicleEnter==true
Your answer
Follow this Question
Related Questions
could someone help with entering/exiting a vehicle? 0 Answers
Enter\Exit Car With Doors. 0 Answers
JS Enter Exit with NetworkPlayer 1 Answer
How to make an Enterable/Exitable Vehicle 1 Answer
vehicle enter/exit, player position problem after exiting 0 Answers