- Home /
How to identify touches via area
Hi, is there a way of storing a certain part of the touch area as a value, similar to FingerId, so that you can control, say 2 individual balls?
Also, is there a way of controlling the cursor, via a small touchpad, within a touch only game? Thanks!
I'm not quite sure to understand what you mean here, could you be more precise ? You can add a canvas and slipt it in two panels (children) that would always be fully transparent and then add an event system on it (it's a component) - a button ins$$anonymous$$d of the panel could work too, but i'm not sure that that's what you want ?
The problem is that doing that still doesn't allow me to control more than 2 axes no matter how many fingers I use, I'm looking for a way to isolate each or at least 2 finger's input in a way that they don't have to both be on at the same time, but that is relative to the position on screen. The idea being controlling separate objects that mirror each finger, but are not dragged. Thanks again.
Answer by ozturkcompany · Apr 03, 2015 at 11:49 AM
I might have been understood your question. OK, lets split out the screen to 2 parts vertically. I assume that you know how to use Input.touches array. You need to check the phase of your touch first and where the touch has been made(position on the screen). The first of a touch will always be touch phase began, so when a touch has been made and if it was in the left part of the screen, call that touch a name, something like touch_L. So you now know that there is a touch on the left parf of the screen. So do a if statament and say like, if touch_L.position.x < Screen.width/2(which means touch is on the left part of the screen) //Do some coding. I hope you understand the logic. Hope this helps out.
Right, it's just that I don't understand how to link an object's XY movement to an if statement, which is why I was hoping I could add something like touch_L to any object I wanted and it would only read that. Once I find the object, how do I declare I want to update it's position relative to my left hand finger, most likely at varying proportion?
if (Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.$$anonymous$$oved)
{touchDelta = Input.GetTouch(0).deltaPosition;}
This is what I had before, and trying to split the screen just moved both objects simultaneously.
#pragma strict
private var TouchID : int = -1;
function Update ()
{
if(Input.touchCount > 0)
{
for(var touch : Touch in Input.touches)
{
if(touch.phase == TouchPhase.Began)
{
TouchID = touch.fingerId;
}
if(touch.phase != TouchPhase.Ended && touch.fingerId == TouchID)
{
if(touch.position.x < Screen.width/2)//Touches are on the left side of the screen
{
//Do something
}
if(touch.position.x > Screen.width/2)//Touches are on the right side of the screen
{
//Do something
}
}
if(touch.phase == TouchPhase.Ended)
{
TouchID = -1;
}
}
}
}
Hope this works! Please guru's check the script that i've written above as it might have some problems. Cheers
Never wrote in java before, but I've been looking for an excuse to start anyway, so yeah thanks a lot!
C# translation from @ozturkcompany:
using UnityEngine;
using System.Collections;
public class Container$$anonymous$$anager : $$anonymous$$onoBehaviour
{
private int TouchID = -1;
void Update ()
{
if(Input.touchCount > 0)
{
for(Touch touch in Input.touches)
{
if(touch.phase == TouchPhase.Began)
{
TouchID = touch.fingerId;
}
if(touch.phase != TouchPhase.Ended && touch.fingerId == TouchID)
{
if(touch.position.x < Screen.width/2)//Touches are on the left side of the screen
{
//Do something
}
if(touch.position.x > Screen.width/2)//Touches are on the right side of the screen
{
//Do something
}
}
if(touch.phase == TouchPhase.Ended)
{
TouchID = -1;
}
}
}
}
}
Sweet, never expected such helpfulness, and of course I over-thought the issue, didn't stop it from consu$$anonymous$$g me though!