- Home /
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.
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.
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.
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.
It's not this problem, my computer has a touch enable monitor.
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.
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?
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?
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.
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.
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?
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.