• 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 Tehsmeely · Jul 28, 2013 at 03:21 AM · keypress

Keypress detection script locations.

I am currently shuffling my way through making a game in Unity and learning as I go.

Looking at handling key-press detection, Placing the "Input.GetKeyDown("up")" in the script that will carry out the triggered process makes sense, but if this is spread over many objects and scripts, is it better to have a single script/object to handle key-presses and then trigger functions in other scripts?

e.g. one script watches WASD in Update() for camera movement, one monitors UpDownLeftRight for character movement. OR. one script monitors all and will send command to camera script or character script when key pressed.

Thanks

Comment
Add comment · Show 3
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 Jamora · Jul 28, 2013 at 04:03 AM 0
Share

Welcome to the world of program$$anonymous$$g. There is no correct/best answer, only ones you are willing/able to implement...

Because this question is quite subjective and argumentative, the forums might have been a better place. I'll throw in my 2cents anyway, in case this gets closed:

I think it's best to have each script have one responsibility; which means that you have one script monitoring for all input then using e.g. events or the slower Send$$anonymous$$essage to tell the interested parties of the occurred event. This practice, in my opinion, makes it easier to make changes without (as much) fear of breaking the rest of the code, because all the relevant logic is in one place.

avatar image highpockets · Jul 28, 2013 at 05:03 AM 0
Share

$$anonymous$$y belief is that it is great having one script manage most of the game control. I'll be quick here...

The easiest way I have come across to talk between scripts without having to find the game object and GetComponents dadadadadada... Is simply by changing the layer name or tag. If this object is infront of that object then that.gameObject.layer = 9 on the game control script, and if you press up and that object is on layer 9, do something in the objects 'todo' script.

It's a pretty easy way to talk to objects from script to script.

avatar image coward · Jul 29, 2013 at 06:24 AM 0
Share
  • for Jamora's response. I'm not sure of the speed of Send$$anonymous$$essage, so it may even be worth another messaging option.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

18 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Can't get simple script to work? 4 Answers

Blocking one button's function when other pressed 1 Answer

When the player presses a key, an object turns invisible? 1 Answer

Why is there a delay before the script functions? 3 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