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 Oct 22, 2013 at 07:41 PM by meat5000 for the following reason:

The question is answered, right answer was accepted

avatar image
3
Question by HunterKrech · Mar 20, 2013 at 05:28 AM · c#androidtouchtouchscreentouch screen

How to get position of touch on touch screen

Hi, kind of new to unity android. Anyway i was wondering how i could get the position of a touch on the screen without it being in pixels cordinates. Here is the code I was trying.

if(Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)

{

     fingerPos =  Input.GetTouch(0).position;
         
     transform.position = fingerPos;

}

I had this attatched to a cube, and of course it flew off screen. Thanks for any help in advance!

Comment
Add comment · Show 3
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 liamcary · Mar 20, 2013 at 05:42 AM 0
Share

What units do you want the co-ordinates in? If you want each axis as a 0 to 1 value you could calculate that by dividing by Screen.width and Screen.height.

avatar image HunterKrech · Mar 20, 2013 at 05:54 AM 0
Share

yeah, but I wanted to get real x and y positions in space

avatar image liamcary · Mar 20, 2013 at 06:08 AM 0
Share

Ok, if it's 2D you could use the camera's world position and offset it by the touch position. But assu$$anonymous$$g this is 3D, you'll probably want to use the 'picking' technique. If you touch the screen, the position in the world could be anywhere in the world along a direct, infinite line into the screen.

If you're not familiar with picking, you'll basically cast a ray from the touch position, directly into the screen until you hit the desired object. In that case, the world position will be the object's position. A good example you probably have seen in games is selecting a unit in a strategy game. You hover over a unit with a mouse and click, and it selects the unit. The mouse cursor isn't necessarily at the unit's position, it's just at some arbitrary point on the screen, but that's how you find what's directly under the mouse. The same logic applies to touch.

I've written a mouse picking script that would be very easy to adapt to touch if you're interested.

1 Reply

  • Sort: 
avatar image
7
Best Answer

Answer by robertbu · Mar 20, 2013 at 06:08 AM

Since you are dealing with a perspective camera, the world coordinates will depend on the distance from the camera into the scene. So figure out the distance you want to use, then use Camera.ScreenToWorldPoint() to convert the value. For example, say you wanted it 8 units in front of the camera:

 var pos : Vector3 = fingerPos;
 pos.z = 8;
 var realWorldPos = Camera.main.ScreenToWorldPoint(pos);

I add this because you said C#:

 Vector3 pos = fingerPos;
 pos.z = 8;
 Vector3 realWorldPos = Camera.main.ScreenToWorldPoint(pos);


Comment
Add comment · Show 2 · 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 burtonposey · Oct 22, 2013 at 07:38 PM 1
Share

The Camera.main should be Camera.main. Other than that, great answer. Thanks for the help.

avatar image Suraj. · Aug 13, 2015 at 06:08 AM 1
Share

Hello...Hi there. What if my camera is rotated?

Follow this Question

Answers Answers and Comments

12 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

Related Questions

how use specific for mobile button when we have 3 ? 1 Answer

Touch Not Working 0 Answers

multi touch / swipe issues 5 Answers

Make an Object follow your touch 0 Answers

Multiple Cars not working 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