Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by FatallErroR · Jun 25, 2013 at 10:51 PM · keyboardkeypresskeydown

Forcing GetKeyDown by script.

Hello everyone,

I would like to know if there's a way to force the player to press a determinate key by script, for example, i want the player to automatically press "c" when he is dead, just want to know is this is possible.

Thanks in advance.

Comment
Add comment
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by Linus · Jun 26, 2013 at 01:54 AM

As darthbator said, you do not actually simulate an input, you just do what would be done if the key was pressed. But here is an example on how you could do it.

 //When the player dies, set this variable to true
 var playerJustDied : Boolean = false;
 function LateUpdate() {    
         //If c is pressed, or player just died
     if(Input.GetButton("c")  || playerJustDied){
           keyCpressed();
           //Set it to false again, so c is not pressed over and over
           if(playerJustDied)
            playerJustDied = false;
           } 


     }
 }
 
 function keyCpressed(){
  Debug.Log('Do what should be done when c is pressed or player died');
 }
Comment
Add comment · Show 2 · 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 Linus · Jun 26, 2013 at 02:34 AM 0
Share

What I would normally do ins$$anonymous$$d of this example, is simply call the keyCpressed() as soon as the player is declared dead. As suggested by darthbator

avatar image FatallErroR · Jun 27, 2013 at 12:13 AM 0
Share

Thanks Linus, this piece of script helped me a little bit. thank you so much.

avatar image
2

Answer by darthbator · Jun 26, 2013 at 12:09 AM

I'm a little confused by the phrasing of the question. Do you want to script to provide the input or do you want to only continue if the user supplies the desired input???

If you don't desire the player to actually give the game input you shouldn't use the input system at all. For example in this case I would just call the method invoked by pressing C after the players "deathEvent" or as it's last instruction.

Comment
Add comment · Show 2 · 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 FatallErroR · Jun 26, 2013 at 01:41 AM 0
Share

Thanks for you reply darthBator.

i just want to force the input to press "c" automatically whenever the character is dead, even if the player is not pressing anything. Since the Input.Get$$anonymous$$eyDown function is reading only, i don't know how i'm gonna do it. ^)

avatar image evand · Jun 26, 2013 at 03:01 AM 0
Share

You do not need to use the input system. If pressing "c" fired a weapon and you wanted that weapon to fire via script (meaning without the player pressing "c"), you would do so via the script.

You could do this via variables and if statements (make variable = 1, if player dies add one to the variable, if variable = 2 then do whatever "c" was supposed to do than subtract one) or more simply by using a boolean and toggling between false and true.

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

19 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

Related Questions

Keyboard Input with a textbox 1 Answer

Numpad Keypress not being registered on mobile phone. 0 Answers

Moving ship to a point on keydown 2 Answers

How to check if key is pressed 1 Answer

Panel isn't opening after hitting the tab key, and its not even saying I pressed the key? 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