- Home /
two input touch android
Hi everybody i'm creating a game that have two touch controls, my problem is !if i input two touches in my screen i can't control the two objects anymore I'm stucked here for 24 hours and still can't find the answer.
Object 1 & Object 2 have a script:
private var dist : float;
private var toDrag : Transform;
private var dragging = false;
private var offset : Vector3;
function Update () {
var v3 : Vector3;
if(Input.touchCount!=1)
{
dragging=false;
return;
}
var touch: Touch = Input.touches[0];
var pos : Vector3 = touch.position;
if(touch.phase==TouchPhase.Began) {
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(pos);
if(Physics.Raycast(ray,hit) && (hit.collider.tag == "Drag"))
{
toDrag = hit.transform;
dist = hit.transform.position.z - Camera.main.transform.position.z;
v3 = new Vector3(pos.x,pos.y,dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.position - v3;
dragging = true;
}
}
if (dragging&&touch.phase==TouchPhase.Moved) {
v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
toDrag.position = v3 + offset;
}
if (dragging && (touch.phase==TouchPhase.Ended||touch.phase==TouchPhase.Canceled)) {
dragging = false;
}
}
Answer by zharik86 · Sep 02, 2014 at 07:18 AM
If you have multitouch events for two or more objects in your scene, that in your script when there is the second touch the first shall work incorrectly. You need to remember the Id of a finger of a contact. I will a little add your script with comments:
private var dist : float;
private var toDrag : Transform;
private var dragging = false;
private var offset : Vector3;
private var finId : int = -1; //Id finger which touch of screen
function Start() {
Input.multiTouchEnabled = true; //enabled Multitouch
}
function Update () {
var v3 : Vector3;
//check all touch events
for(var touch : Touch in Input.touches) {
var pos : Vector3 = touch.position;
if(touch.phase == TouchPhase.Began && finId == -1) { //add check for finger Id
var hit : RaycastHit;
var ray : Ray = Camera.main.ScreenPointToRay(pos);
if(Physics.Raycast(ray,hit) && (hit.collider.tag == "Drag")) {
toDrag = hit.transform;
dist = hit.transform.position.z - Camera.main.transform.position.z;
v3 = new Vector3(pos.x,pos.y,dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
offset = toDrag.position - v3;
dragging = true;
finId = touch.fingerId; //store finger Id our touch
}
}
if (dragging && touch.phase == TouchPhase.Moved && finId == touch.fingerId) {
v3 = Vector3(Input.mousePosition.x, Input.mousePosition.y, dist);
v3 = Camera.main.ScreenToWorldPoint(v3);
toDrag.position = v3 + offset;
}
if (dragging && fingId == touch.fingerId && (touch.phase == TouchPhase.Ended||touch.phase == TouchPhase.Canceled)) {
dragging = false;
finId = -1;
}
}
}
In my opinion, it is better for you to use GUITexture and to work with it. You watch an example of it in my pregoing response. I hope that it will help you.
Thanks for that sir zharik! $$anonymous$$y problem now is how can i drag those guitexture depending in my finger position? @zharik86
I will write a small example of a script where GUITexture follows a finger on the screen:
var myTexture: GUITexture;
private var firstPosTouch: Vector3 = Vector3.zero; //position when touch began
private var firstPosTex: Rect; //position texture when touch began
private var finId: int = -1; //Id finger of touch
function Start() {
Input.multiTouchEnabled = true; //enabled $$anonymous$$ul$$anonymous$$ch
}
function Update() {
for (var touch : Touch in Input.touches) {
var pos : Vector3 = touch.position;
if (touch.phase == TouchPhase.Began && myTexture.HitTest (touch.position) && finId == -1) {
finId = touch.fingerId; //store Id finger
//Store init position
firstPosTouch = pos;
firstPosTex = myTexture.pixelInset;
}
if (touch.phase == TouchPhase.$$anonymous$$oved && finId == touch.fingerId) {
//Change pos texture
var offsetX: float = (pos.x - firstPosTouch.x) + firstPosTex.x;
var offsetY: float = (pos.y - firstPosTouch.y) + firstPosTex.y;
myTexture.pixelInset = Rect(offsetX, offsetY, firstPosTex.width, firstPosTex.height);
}
if (fingId == touch.fingerId && (touch.phase == TouchPhase.Ended||touch.phase == TouchPhase.Canceled)) {
finId = -1;
}
}
}
Your answer
Follow this Question
Related Questions
Click Two objects At Once Windows 7 Touch 1 Answer
IOS object touch, collider 1 Answer
Pinching invokes SingleTouch 1 Answer
Problem getting touch input 0 Answers
index out of bounds 6 Answers