- Home /
From Input.GetMouse to Input.touch. Multi-touch and raycasts?
Hi Guys. So I'm trying to figure out how to use Input.Touch. Usually I use Input.GetMouseButton(0) and draw a raycast to where ever the person touched on the screen.
Now what I'm trying to do is make it so the player can touch multiple buttons at once. The arrows buttons and any other button the same time. I want it so you can press anything in Box 1 as well anything from the other Box 2s at the same time. I've tried looking at similar answers but am quite confused about how to work multi-touch controls.
So I think what I need is to draw separate raycasts from separate finger touches but I'm not really sure. I heard raycasts are quite expensive so this may not be the best option for mobile controls. Any help is appreciated and I'll attach the relevant code I have so far. The code below is used pretty much on both scripts but with different hit.collider.names.
Thanks in advance for any assistance!
private var ray : Ray;
private var hit : RaycastHit;
function Update()
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
Click1();
}
function Click1(){
if(Input.GetMouseButton(0)){
if(Physics.Raycast(ray, hit,100f)){
if (hit.collider.gameObject.name == "RightArrow"){
//DoSomething
}
}
}
}
Start with the sample scripts here:
https://docs.unity3d.com/Documentation/ScriptReference/Input.GetTouch.html
Thanks the third example looks like what I need I'm just having a little bit of trouble making out how they work. I'm guessing if(Input.GetTouch(i).phase == TouchPhase.Began) is somewhat like (Input.Get$$anonymous$$ouseButton(0))? then perhaps I can change (i) between (0) and (1) for multiple touches?
Actually I just found a tutorial that seems to be explaining the things I'm confused about. If anybody else is interested this has helped: http://forum.unity3d.com/threads/202940-Unity-Touch-Input-Tutorials
Answer by brzydal · May 19, 2014 at 01:26 AM
Hi, try this:
function Update()
{
if(Input.touchCount > 0)
{
for(var i:int = 0; i<Input.touchCount; i++)
{
if(Input.touches[i].phase==TouchPhase.Ended)
{
var ray:Ray = Camera.main.ScreenPointToRay(Input.touches[i].position);
var hit:RaycastHit;
if(Physics.Raycast(ray, hit, 100f))
{
Click(hit.collider.gameObject.name);
}
}
}
}
}
function Click(name:String)
{
if(name.Equals("RightArrow"))
{
//DoSomething;
}
else if(name.Equals("SomethingElse"))
{
//DoSomethingElse
}
}
I'll integrate it tomorrow and tell you how it works, I just looked at a few other sources and think I understand touch input a lot better now, thanks very much!
Your answer

Follow this Question
Related Questions
How to get multitouch input using EasyTouch 0 Answers
Unusual multitouch behavior (Android) 3 Answers
How to use multi touch 0 Answers
Finger Manager position 1 Answer
uniTUIO touch delay 0 Answers