Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Noxury · Jul 19, 2015 at 10:12 AM · inputvehiclegetkeydownexitenter

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.

Comment
Add comment · Show 2
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Positive7 · Jul 19, 2015 at 10:27 AM 1
Share

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();
 }

avatar image Noxury · Jul 19, 2015 at 11:02 AM 0
Share

thanks, but in the else part I control the playermovement, which needs to run every frame, so exitVehicle() runs also every frame.

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Noxury · Jul 20, 2015 at 11:30 PM 0
Share

thank you all this works now :)

avatar image
0

Answer by 334499p · Jul 20, 2015 at 07:00 PM

Set the first script to GetKeyUp

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

26 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges