• 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
Question by dnadevelopers · Aug 22, 2014 at 05:55 PM · androidmovementinputtouchbuttons

Help with touchscreen controls

Hi everybody! I'm new to Unity and to scripting... I'm trying to make a small game for android but I'm having some trouble with the touchscreen movement, it works fine with the keyboard on the PC. I only need to move my player left and right. I have tried lots of scripts found on the Internet but none seemed to work... My original idea was to divide the screen in half and make each half a button. Another way I t$$anonymous$$nk is to make two buttons for right and left, I have tried but I can't understand how to do it. I use C# but Javascript is fine as well. If you could show me how you'd do it, it will be very much appreciated. Thanks in advance. P.S.: I know how to move the player, I just don't know how to use touchscreen input.

Comment

People who like this

0 Show 0
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
Best Answer

Answer by Landern · Aug 22, 2014 at 05:56 PM

 if (Input.touchCount == 1)
 {
     Touch[] touch = Input.touches[0];
     if (touch.position.x < Screen.width/2)
         // Somet$$anonymous$$ng that does what you need if left side is touched
     else if (touch.position.x > Screen.width/2)
         // Somet$$anonymous$$ng that does what you need if rightside is touched
 }
Comment
Karios

People who like this

1 Show 4 · 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 daveinpublic · Aug 22, 2014 at 06:05 PM 0
Share

I have a question - where would you put that script, on the player?

avatar image Landern · Aug 22, 2014 at 06:22 PM 0
Share

Could be, could be on the camera, either way in an Update function/method. You can put it on what ever you want, i don't know your project structure.

avatar image dnadevelopers · Aug 22, 2014 at 07:44 PM 0
Share

Thank you so much it finally works!

avatar image ShazBang · Nov 30, 2014 at 02:39 AM 0
Share

hey, this is the exact code I have been looking for.

I am super noob at this, so why do i get errors like

';' expected. Insert a semicolon at the end.

Unexpected token: else.

Unexpected token: }.

expecting }, found ''.

Please help, where do I put this code? is it Java script or C#?

I'm terrible at this.

avatar image

Answer by luniac · Nov 30, 2014 at 02:51 AM

try TouchScript, its free and worked great for me after around 30 hours of fiddling around to learn how it works.

https://touchscript.github.io/

u could totally create an invisible plane for the left side of the screen and one for the right side. u can attach a press gesture to each plane and have the callback move the character left or right depending on w$$anonymous$$ch plane was pressed. What's nice is that you can also test t$$anonymous$$s with mouse because touchscript has a built in emulator for it.

Alternatively you can have a full screen layer for the camera and a press gesture on it. Whenever any press is detected simply mathematically check whether its to the left or to the right of the midpoint of the screen, and then move the character accordingly.

I like second solution better since you don't have to make any extra objects.

Comment

People who like this

0 Show 0 · 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.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Moving character with touch buttons (Android) 1 Answer

Replace Input.GetAxis with Touch 2 Answers

Android multitouch prevent when begin on UI issue 0 Answers

Android - touch a 3d object and something happens to it 1 Answer

Touch Input madness 2 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