Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by shadowpuppet · Apr 20, 2018 at 07:27 PM · keydown

(Input.GetKeyDown("space")) or (Input.GetKeyDown(KeyCode.Space))?

The scriptingAPI page it lists both. Is one better than the other? I just found out after writing if(other.tag == "Player") for months that if(other.CompareTag == ("Player")) is more efficient... so is one of the keyDown versions better than the other?

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

1 Reply

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

Answer by Hellium · Apr 20, 2018 at 08:27 PM

Input.GetKeyDown( string ) requires a string defined in the InputManager. So, if your InputManager is "empty", this function won't work. Without mentionning that space can be mapped with an other key than the spacebar (in the input manager or by the user if you have enabled the configuration box)


Input.GetKeyDown( KeyCode ) requires a specific key, no matter what.


Using the KeyCode version is clearly more efficient than the string version, but is less flexible.

Comment
Add comment · Show 6 · 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 shadowpuppet · Apr 20, 2018 at 09:36 PM 0
Share

Great thanks. $$anonymous$$akes perfect sense. I 'm still in shock how the CompareTag being more efficient than .tag== "string". Is this fairly new ( post version 3.4)? Because I learned on if (other.tag == "Player") which I think I got from the S$$anonymous$$lth tutorial. I didn't know ANYTHING about coding back then so doubt I would have come up with it on my own.

avatar image Hellium shadowpuppet · Apr 21, 2018 at 06:05 AM 0
Share

According to the doc, CompareTag is available since Unity 4.0.


There are several optimization tricks in Unity you will learn little by little. For example the static function Animator.StringToHash creates a hash of the specified string parameter so that you can call Animator.SetTrigger (or any other SetXXX) with this hash ins$$anonymous$$d of the string, which is also more efficient.

avatar image shadowpuppet Hellium · Apr 21, 2018 at 06:13 AM 0
Share

Thanks, that is another script tip I appreciate. All may animations are ani.SetTrigger just like all my trigger enters are (other.tag=="Player"). Not that I experience any lags in a build but even if it buys me 1 FPS it's worth gradually upgrading as I revisit scripts

Show more comments

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

133 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Multiple keydowns/coroutines cause unwanted angles in objects 0 Answers

How to check if the pressed key is different than the expected key ? 2 Answers

Input.inputString and Input.GetKeyDown do nothing 0 Answers

How do you make a particle effect play on a key press? 0 Answers

C# Load Level after seconds+key press 1 Answer


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