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 /
avatar image
1
Question by yyykobe · Jul 08, 2013 at 03:31 PM · inputpositiontouchscreengettouch

Input.GetTouch(0) doesn't work

Hello, i am stuck in this problem. i have tried the input.getbuttondown(0), and it works. Now, what i want to do is get several touch position, so i use Input.GetTouch(0). However, it doesn't work. i am confused because i am following the documentation and answers in unity.

         Plane horPlane = new Plane(Vector3.up, Vector3.zero);
         // get the ray from the camera...
         Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(0).position);
         print("mouseposition="+Input.GetTouch(0).position);
         //startpos=Input.mousePosition;        
         float distance1 =0;
         // if the ray intersects the logical plane...
         if (horPlane.Raycast(ray, out distance1)){
           cube.transform.position = ray.GetPoint(distance1); // move cube to the 3D point clicked
           print("cube position="+cube.transform.position);
         //}
     }


Thanks all of you.

Comment
Add comment · Show 8
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 Jake.OConnor · Jul 09, 2013 at 08:44 PM 1
Share

What does "it doesn't work" mean? Is it throwing an error because there are no touches? Is it returning strange values?

One blatant flaw in the portion of the script you posted was that there's no guard against accessing touches that don't exist. Always make sure there is at least one touch registered before you try to access it.

avatar image yyykobe · Jul 10, 2013 at 07:29 AM 0
Share

Thanks for your answer, Jake. when i use "Input.GetTouch(0)", and run the exe, i touch the screen, it does nothing. Also, when i check the outlog file, there is no cube position output, then i know the Input.GetTouch(0) doesn't work at all. there is no error in the script. In fact, i never successfully use Input.GetTouch(0). It's very strange.

avatar image Eugenius · Jul 10, 2013 at 07:34 AM 0
Share

Do you run the app on your computer? If yes and your computer does not have a touch enabled monitor then it will not work. To test it, you need to release an .apk or .ipa builds to test on android devices or apple products.

avatar image yyykobe · Jul 10, 2013 at 07:39 AM 0
Share

It's not this problem, my computer has a touch enable monitor.

avatar image Eugenius · Jul 10, 2013 at 07:43 AM 0
Share

try Input.GetTouch(0) && Input.touchCount > 0

Show more comments

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by johnnymoha · Jul 10, 2013 at 07:53 AM

Try my solution below just in case, but I'd bet that your problem is that your pc's "touch" input is on a higher level well outside what unity sees and a touch is just being treated as a mouse click in the position that you touched as far as Unity is concerned. Because you were using touches, I thought you were building for android or IOS. I think that's all that Touches are used for.

I see a couple of potential problems. If I'm developing and testing on a computer and deploying full builds occasionally to android, I always use the compiler directive #if UNITY_ANDROID and #if UNITY_EDITOR and UNITY_WIN_STANDALONE to seperate my different kinds of inputs for different builds. If you're running an exe I don't think you're going to have any "touches". That's why your mouse portion worked and your touch portion didn't. As the person above me said, you'll want to check the size of input.touchcount to make sure you've even got a touch before trying to reference it. So quick demo:

function Update () { #if UNITY_ANDROID if(Input.touchCount ==1){ clickPos = Input.GetTouch(0).position; } #endif #if UNITY_EDITOR or #if UNITY_STANDALONE_WIN if (Input.GetMouseButtonDown (0)) { //If Click clickPos = Input.mousePosition; } #endif COMMON CODE HERE THAT MAKES DECISIONS BASED ON clickPos which gets set different ways depending on what platform you built for.

Comment
Add comment · Show 7 · 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 yyykobe · Jul 10, 2013 at 07:59 AM 0
Share

Thanks Johnnymoha, i build the platform on PC. what you mean is that i can only use Input.touchcount these function in building Android or IOS platform?

avatar image johnnymoha · Jul 10, 2013 at 08:05 AM 0
Share

Yes, I believe that's why you can use mouse input and not the touch input. If you touched the screen with the mouse button code left in does it work?

avatar image yyykobe · Jul 10, 2013 at 08:11 AM 0
Share

yes, if i use Input.getmousebuttondown(0), then i find when i use one finger touch on the screen , it's the same effect as i use mousebutton click.

avatar image johnnymoha · Jul 10, 2013 at 08:28 AM 0
Share

ok, that's what I though. YOu PC treats a finger touches like a mouse click and Unity only know what your pc tells it. You'll be needing to use the mouse functions ins$$anonymous$$d of input.touch in your code.

avatar image yyykobe · Jul 10, 2013 at 08:51 AM 0
Share

Thanks a lot. that makes me crazy, because i want to detect mul$$anonymous$$ch on the screen. Now i can only know one finger position, i am looking for the solution to get the second finger position. Do you have any idea in detect other finger position using the mouse function?

Show more comments
avatar image
0

Answer by nark · Oct 27, 2016 at 12:40 PM

Do you receieve the input in Update() or FixedUpdate() ? If FixedUpdate, try move it to Update() instead.

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

19 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

Related Questions

Input Touch.position 'inverted' relative to screen? 1 Answer

unity 5.2 Input.GetTouch(0).position incorrect? 3 Answers

Touch.position, Touch.deltaPosition, Screen.width, Screen.Height 1 Answer

Movement along with touch of finger 2 Answers

vector2 is 0 to 1? 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