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 tekkar · Feb 13, 2016 at 01:16 PM · c#androidtouchgetmousebuttondown

Problem with simple Character movement on Android (Touch/GetMouseButtonDown)

Hello, i have a problem with GetMouseButtonDown and Android Touch. i have this code, which works fine on Pc. (C# by the way)

   if (Input.GetMouseButtonDown(0) && !isDead)
     {
         playing = true;
         score++;
         scoreText.text = score.ToString();
         speed += 0.08f;
       
         if (dir == Vector3.forward)
         {
             dir = Vector3.left;
         }
         else
         {
             dir = Vector3.forward;
         }
     }

 if (Input.GetMouseButtonDown(1) && !isDead)
     {
         playing = true;
         score++;
         scoreText.text = score.ToString();
         speed += 0.08f;

         //Switches the players direction every time we click ont he screen or mouse
         if (dir == Vector3.forward)
         {
             dir = Vector3.right;
         }
         else
         {
             dir = Vector3.forward;
         }
     }


which does:

When you left click, he moves forward, if you left click again he goes to the left. if you left click again he goes forward etc.

if you right click he moves forward, if you right click again he goes to the right. if you right click again he goes forward etc.

but when i try it on my Android phone, it only works with GetMouseButtonDown(0) and not (1) so every time i click on the screen, no matter where, the character goes left, if you click again he goes forward.

is there any way to make mousebuttondown(0) half of the screen and mousebuttondown(1) the other half? then it would work.

i tried to do this:

 foreach (Touch touch in Input.touches)
     {

         if (touch.position.x < Screen.width / 2) 
         {
             if (Input.GetMouseButtonDown (0) && !isDead) {
                 playing = true;
                 score++;
                 scoreText.text = score.ToString ();
                 speed += 0.08f;
                 //Switches the players direction every time we click ont he screen or mouse
                 if (dir == Vector3.forward) {
                     dir = Vector3.left;
                 } else {
                     dir = Vector3.forward;
                 }


             }

         } 
         else if (touch.position.x > Screen.width / 2) 
         {

             if (Input.GetMouseButtonDown (1) && !isDead) 
             {
                 playing = true;
                 score++;
                 scoreText.text = score.ToString ();
                 speed += 0.08f;

                 //Switches the players direction every time we click ont he screen or mouse
                 if (dir == Vector3.forward)
                 {
                     dir = Vector3.right;
                 }
                 else 
                 {
                     dir = Vector3.forward;
                 }
             }

         } 
     }


but it still dont work correctly. I dont want the player to be able to "hold" his finger on the screen, it should only register one click like on the mouse, untill the finger is lifted and the next touch is registered.

Im still bad at Unity so im sorry if this is a stupid question or hard to understand.

Any tips would be appreciated. Tekar

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 tekkar · Feb 13, 2016 at 11:47 PM

fixed it by making Input.GetMouseButtonDown (1) to (0) on the other half of the screen aswell

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

Follow this Question

Answers Answers and Comments

105 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

Related Questions

UNITY 2D ANDROID DEVICE TOUCH PROBLEM 0 Answers

Android Controls 1 Answer

Jump by touch Unity C# 0 Answers

Player follow touch when holding down your finger on the screen (C#) 1 Answer

Multitouch doesn't work 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