Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
This question was closed Aug 31, 2015 at 09:11 PM by Ashky for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Ashky · Jun 17, 2014 at 11:13 AM · android2dtouch

How to continuously detect the exact location of a touch on Android?

Hello Unity Community!

I'm working on a 2D Android game, and I'm having a bit of trouble detecting touches. What I'm trying to do is something along the lines of this:

     while(touch.position.x>Screen.width/2)
     {
     MoveRight();
     }

I need this to be continuous, i.e. as long as the right side of the screen has a touch on it, keep moving the character.

I've tried modifying a Unity example, but it didn't work out as expected.

So, my questions:

  1. How is the proper way to do this?

  2. Does it matter if the code is in Update() or FixedUpdate()? I'm using 2D Physics.

  3. Does Unity Remote affect in any way touch detection ?

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

  • Sort: 
avatar image
0
Best Answer

Answer by pstreef · Jun 17, 2014 at 01:19 PM

1 I would try something like the example below

2 Using Update is better for input, because it is called every frame. FixedUpdate is for physics stuff. (read up on it here)

3 Not as far as I know.

 public float movementSpeed = 1.0f;
         
 void Update()
 {
     if(Input.touches.Length() > 0)
     {
         Touch touch = Input.touches[0];
         if (touch.phase != TouchPhase.Ended && touch.phase != TouchPhase.Canceled)
         {
             if(touch.position.x>Screen.width/2)
             {
                 Vector3 oldPos = transform.position;
                 float newXPos = oldPos.x + movementSpeed * Time.DeltaTime;
                 transform.position = new Vector3(newXPos,oldPos.y,oldPos.z);
             }
         }
     }
 }
 
 
 


Comment
Add comment · Show 3 · 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 Ashky · Jun 17, 2014 at 01:33 PM 0
Share

Thank you for your great answer. This seems to work very well, except for the fact that I'm getting a "IndexOutOfRangeException: Array index is out of range." error on the Touch touch = Input.touches[0]; line. Any idea what's up with that ?

avatar image pstreef · Jun 17, 2014 at 01:46 PM 0
Share

Yes, I changed the answer with a fix. Not sure if the syntax is 100% correct, but the idea should work. The problem was when there is no touches the Input.touches array has a length of 0 so index 0 is out of range.

avatar image Ashky · Jun 17, 2014 at 02:00 PM 0
Share

if(Input.touchCount > 0) works. Thank you very much!

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

I Need Help With Mobile Touch Movement. 0 Answers

Best way to do touch in with Unity2D? 0 Answers

Please can someone explain how i use the joystick prefab for 2d. 0 Answers

some android phone's touch won't work sometimes with my game app, but it works perfectly in my phone. 0 Answers

Move horizontally on touch 0 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