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
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.
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.
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