Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 S_Byrnes · Mar 09, 2014 at 03:05 AM · androidinputtouchkeycodeinput setting

Touch buttons to replace Input GetKey KeyCode

Hey guys, I really need help here, I'm creating a constantly moving 2D game, it's functioning fully in unity and for pc, but I've made it for mobile devices, I've looked everywhere online and around here for an answer but haven't found one that works in my situation

here is the segment of my code that contains button presses:

 if (Input.GetKey(KeyCode.A) || Input.GetKey(KeyCode.LeftArrow))
         {
             rigidbody.AddRelativeTorque(0,0,1.5f);
         }
         if (Input.GetKey(KeyCode.D) || Input.GetKey(KeyCode.RightArrow))
         {
             rigidbody.AddRelativeTorque(0,0,-1.5f);
         }

I have made a couple of sprites that I intend to replace the keyboard left and right for touch inputs, I just need to know how to code it using my current code.

Any ideas/help would be greatly appreciated!

Thanks.

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 fifthknotch · Mar 09, 2014 at 05:25 AM

First thumbs up for one of the best written questions I've had to privilege to answer. You need to study the Unity docs for mobile input. That is found here:

http://docs.unity3d.com/Documentation/Manual/Input.html

Particularly check out GUI hittest in the script reference.

here is a code snippet of how i used this to make my game pause when the user pressed the pause "button" (it was actually just a texture that represented 2D space input could be read in as clicking pause):

 #pragma strict
     var pauseButton : GUITexture;
     var isPaused = false;
     var timer : float = 0.0;
     
     function Start () {
         isPaused = false;
     }
     
     function Update () {
             // This starts a timer
         if (timer <= 1.0)
             timer = timer + .1;
             
         var count : int = Input.touchCount;
         for (var touch : Touch in Input.touches) {
             if (pauseButton.HitTest( touch.position )) {
                 // This checks that timer to make sure it has given the user enough time
                 // to lift their finger before it cycles through the remaining code again.
                 if (timer >= 1.0) {
                     if (isPaused) {
                         Time.timeScale = 1;
                         isPaused = false;
                     }
                     else {
                         Time.timeScale = 0;
                         isPaused = true;
                     }
                     timer=0.0;
                 }
             }
         }
     }
Comment
Add comment · Show 33 · 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 S_Byrnes · Mar 09, 2014 at 05:32 AM 0
Share

Thanks for the response and I'm glad you liked how I asked my question.

The link you posted, I've been studying that page all day and I can't figure out how to use a sprite in the same context as I have my code currently in

I really appreciate the code you provided, that may come in handy to me later, creating a pause menu was next on my to-do-list.

avatar image fifthknotch · Mar 09, 2014 at 05:37 AM 0
Share

So did this answer your question? Or do you have another?

avatar image S_Byrnes · Mar 09, 2014 at 05:40 AM 0
Share

Not exactly, I still can't figure out how to implement a touch control ins$$anonymous$$d of keyboard left and right, do you know how I might add it to my code?

I really appreciate your time to help me here

avatar image fifthknotch · Mar 09, 2014 at 05:54 AM 0
Share

Do you want me to create your code for you? Or would you like to play around with the script I just gave you? I have it all worked out for what to change here and there in your code to get your input working, but I don't feel that that is the best way for me to help you.

This is the problem with me just writing your code for you: once you get the code I write to work, you won't understand how it worked or what I did to make it work. Part of learning is you need to understand why something works. Sometimes suffering over finding the solution to a problem is a good thing because you will always remember how you figured it out once you finally do. :) I can give you some code if you want, but I think you can figure this out...

avatar image S_Byrnes · Mar 09, 2014 at 05:58 AM 0
Share

I would really appreciate it if you gave me the code, believe me, I will definitely know why it works and will remember it, I've slaved over this problem for about 12 hours straight (no breaks), I think it will take me another 24 hours straight to figure it out by myself

It would be great if you could post the code

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

21 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

Related Questions

Touch Inputs Not Allowing Mutiple Presses 1 Answer

How to differentiate touches on mobile devices 1 Answer

Bug? Need to wait about 10 seconds to get back to normal 1 Answer

Joypad help 0 Answers

¿Unity messing up fingerIDs? [Android] 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