- Home /
touchCount not recognized with multitouch
I have a script that uses touchCount and Raycast and when I played it it does not work with multitouch:
if(Input.touchCount > 0) {
hit = Physics2D.Raycast(camera.ScreenToWorldPoint(Input.mousePosition), Vector2.zero);
if(Input.GetTouch(0).phase == TouchPhase.Began){
if( hit != null && hit.collider.gameObject.name == "Right"){
//Other Stuff
}
}
}
Why wouldn't it work with multitouch?
In line 2... Try using ... Input.GetTouch(0).position ... Or ... Input.GetTouch(0).deltaPosition .. Ins$$anonymous$$d of .. Input.mousePosition... As you require ..
Also probably try writing seperate conditions for input.touchCount == 1 and ... Input.touchCount == 2... Probably single touch taking precedence right after touch detection.
Red
If you are writing seperate conditions .. Of course you would have to remove the condition... Input.touchCount > 0.
but I've also got an Input.GetTouch(0).phase == TouchPhase.Ended @Redeemer86
@markzareal ... Input.mousePosition is not equivalent to a second touch if you are using both input.mouseposition and input.gettouch(0) ... Input.getmousebutton(0) is kind of synonymous in functionality to a single touch (input.gettouch(0))on touch devices .. While input.getmousebutton(1) is synonymous in functionality to a double touch (input.gettouch(0) and input.gettouch(1)) ... It would be better if you distinguished between different conditions. And used input.gettouch(0).position for single touch condition and both input.gettouch(0).position and Input.gettouch(1).position for double touch and so on. Using mouse position and gettouch does not Imply double or multi touch.
Red
Can you give me an example how to use Input.mousePosition? I want mul$$anonymous$$ch because I have arrow buttons on my screen so I want the player be able to hold 2 buttons at the same time
Answer by HarshadK · Dec 05, 2014 at 07:39 AM
If you really want to support multitouch then you need to code for each touch. If each touch performs the same action then you can use a for loop as below:
if(Input.touchCount > 0) {
for(int i = 0; i < touchCount; i++) {
hit = Physics2D.Raycast(camera.ScreenToWorldPoint(Input.GetTouch(i).position), Vector2.zero);
if(Input.GetTouch(i).phase == TouchPhase.Began){
if( hit != null && hit.collider.gameObject.name == "Right"){
//Other Stuff
}
}
}
}
What is the output? Can you provide details as to what behavior you are getting?