• 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
2
Question by rocket5tim · Dec 18, 2009 at 04:52 PM · iphone

How to get accurate touch.position?

I'm unable to get a consistently accurate touch.position or hit.point using the following script. Touching the screen prints the following targetLocation to the console:

16.8, 6.1, 0.0
16.6, 6.1, 0.0
9.0, 2.9, 0.0 <-- this one is where I was actually touching!
16.4, 6.2, 0.0

function TouchTarget() { // called once per frame in Update var count : int = iPhoneInput.touchCount; if(count == 1) { var touch : iPhoneTouch = iPhoneInput.GetTouch(0);

     if (touch.phase == iPhoneTouchPhase.Began) {
         //var ray = Camera.main.ScreenPointToRay( Vector3( touch.position.x, touch.position.y ) );
         var ray = Camera.main.ScreenPointToRay(touch.position);
         var hit : RaycastHit;
         if(Physics.Raycast(ray, hit)) {
             targetLocation = hit.point;
             targetLocation.z = 0.0; // ignore z
             Debug.Log(targetLocation);
         }
     }
 }

}

I've also tried getting the touch position in the following ways, but each of them has the same random inaccuracy.

// one way targetLocation = hit.point; targetLocation.z = 0.0; // ignore z

// another way targetLocation = touch.position;

// yet another way var targetLoc : Vector2 = hit.point; targetLocation = Vector3 (targetLoc.x, targetLoc.y, 0);

Any suggestions on where I might be going wrong?

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by rocket5tim · Dec 30, 2009 at 05:52 PM

Turns out that the problem is not with the code, but rather with my scene. Since I'm making a 2D side-scroller, I didn't have any collision on the background (nothing ever moves in Z so why would I need collision there?). After adding a collision box across my background, I am now getting accurate touch.position and hit.point.

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
avatar image
0

Answer by Lucas Meijer 1 · Dec 20, 2009 at 10:40 PM

I'm not very fermiliar with iPhone specifics, but since your question hasn't had an answer yet, I'll shoot anyway: It looks like you're not correctly dealing with the situation where there would be more than one touches. Instead of dealing with all of them, you currently deal with none of them. You only do something if there is exactly one touch.

Comment
Add comment · Show 1 · 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 rocket5tim · Dec 23, 2009 at 10:32 PM 0
Share

In this example yes I'm only allowing 1 touch at a time. But that shouldn't affect the touch position. I suspect the problem might lie in the difference between touch.position and hit.point (both of those return different values). But I'm not sure how to get the "correct" point on screen.

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

1 Person is following this question.

avatar image

Related Questions

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

Problem with video in IOS 1 Answer

How to check what iDevice your using and adjust GUI sizes according... 1 Answer

how to make simple touch commands and play animation? 1 Answer

TPS Camera with Joystick (Android) 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