- Home /
Multitouch Android touches excluding each other
When i press one button lets say left iworks but when i press jump the jump starts but left movement stops.....
using UnityEngine;
using System.Collections;
public class touchControls : MonoBehaviour {
public GameObject player;
public bool movingleft = false;
public bool movingright = false;
public bool grounded;
public float jumpForce = 100;
public int finger1 = 1;
public int finger2 = 2;
public int finger3 = 3;
public int finger4 = 4;
public Touch touch;
Rigidbody2D rig;
void Start () {
rig = GameObject.FindGameObjectWithTag ("player").GetComponent<Rigidbody2D> ();
}
void Update () {
grounded = GameObject.FindGameObjectWithTag ("player").GetComponent<kontrola> ().ground;
if (grounded) {
//sr.sprite = gor;
} else if (!grounded) {
// sr.sprite = dol;
}
if (Application.platform == RuntimePlatform.Android) {
if (Input.touchCount > 0) {
for (int i=0; i<Input.touchCount; i++) {
if(Input.GetTouch (i).phase == TouchPhase.Began){
CheckTouch(Input.GetTouch(i).position, "begin");
}
else if(Input.GetTouch (i).phase == TouchPhase.Ended){
if(touch.fingerId == finger1){
finger1 = 1;
movingleft = false;
}
if(touch.fingerId == finger2){
finger2 = 2;
movingright = false;
}
if(touch.fingerId == finger3){
finger3 = 3;
}
}
}
}
}
if (movingleft) {
player.transform.Translate (Vector2.left * Time.deltaTime * 5f);
}
if (movingright) {
player.transform.Translate (Vector2.right * Time.deltaTime * 5f);
}
}
void CheckTouch(Vector3 pos, string phase){
Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
Vector2 touchPos = new Vector2(wp.x, wp.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if (hit.gameObject.name == "levogor" && hit && phase == "begin"){
movingleft = true;
finger1 = touch.fingerId;
}
if (hit.gameObject.name == "desnogor" && hit && phase == "begin"){
movingright = true;
finger2 = touch.fingerId;
}
if (hit.gameObject.name == "jump" && hit && phase == "begin" && grounded) {
rig.AddForce (new Vector2 (0f, jumpForce));
finger3 = touch.fingerId;
}
}
}
Nope that didnt worked....i will go with other approach with manipulating foreach(Touch t in Input.touches)......
I think there is a problem when you use incrementig touches because update is rearanging array all the time. But for each i think it looks the current content of the array.... There is no sigle right way people are using this have looked all the related prblems here on unity and stark overflow and there is something fishy about this problem... it seems like nobody came to the bottom of it..... BUT I Will....
Now this is funny.... I have deleted the approach with fingerID and i get the same results. When i press the left button i go left when i stop pressing it stops. When i press left button and press somewhere on the screen the left button is interupted and the gameobject stops. if i press left or right i go in the direction i press jump i jump but the movement gest interupted. I think there is a problem because i use outside method and then the update is interupted. i will try to do every thing in update this i think will be the solution.
NeverHopeless thank you for your help. If you have some other hint dont hesitate. The result of the code below is described in the begining. I will post new one a bit later when i try the solution.
using UnityEngine;
using System.Collections;
public class touchControls : $$anonymous$$onoBehaviour {
public GameObject player;
public bool movingleft = false;
public bool movingright = false;
public bool grounded;
public float jumpForce = 100;
public int finger1 = 1;
public int finger2 = 2;
public int finger3 = 3;
public Touch touch;
Rigidbody2D rig;
void Start () {
rig = GameObject.FindGameObjectWithTag ("player").GetComponent<Rigidbody2D> ();
}
void Update () {
grounded = GameObject.FindGameObjectWithTag ("player").GetComponent<kontrola> ().ground;
if (grounded) {
//sr.sprite = gor;
} else if (!grounded) {
// sr.sprite = dol;
}
if (Application.platform == RuntimePlatform.Android) {
if (Input.touchCount > 0) {
for (int i=0; i<Input.touchCount; i++) {
if(Input.GetTouch (i).phase == TouchPhase.Began || Input.GetTouch (i).phase == TouchPhase.$$anonymous$$oved){
CheckTouch(Input.GetTouch(i).position, "begin");
}
if(Input.GetTouch (i).phase == TouchPhase.Ended){
movingleft = movingright = false;
}
}
}
}
if (movingleft) {
player.transform.Translate (Vector2.left * Time.deltaTime * 5f);
}
if (movingright) {
player.transform.Translate (Vector2.right * Time.deltaTime * 5f);
}
}
void CheckTouch(Vector3 pos, string phase){
Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
Vector2 touchPos = new Vector2(wp.x, wp.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if (hit.gameObject.name == "levogor" && hit && phase == "begin" ){
movingleft = true;
//finger1 = touch.fingerId;
}
if (hit.gameObject.name == "desnogor" && hit && phase == "begin" ){
movingright = true;
//finger2 = touch.fingerId;
}
if (hit.gameObject.name == "jump" && hit && phase == "begin" && grounded) {
rig.AddForce (new Vector2 (0f, jumpForce));
// finger3 = touch.fingerId;
}
}
}
Answer by NeverHopeless · Aug 17, 2015 at 12:06 PM
Perhaps instead of IF...ELSE... block just using IFs can fix this, because your code only supports one of them due to IF...ELSE....
Try like this:
void CheckTouch(Vector3 pos, string phase){
Vector3 wp = Camera.main.ScreenToWorldPoint(pos);
Vector2 touchPos = new Vector2(wp.x, wp.y);
Collider2D hit = Physics2D.OverlapPoint(touchPos);
if(hit.gameObject.name == "levogor" && hit && phase == "begin"){
movingleft = true;
finger1 = touch.fingerId;
}
if (hit.gameObject.name == "desnogor" && hit && phase == "begin"){
movingright = true;
finger2 = touch.fingerId;
}
if (hit.gameObject.name == "jump" && hit && phase == "begin" && grounded) {
rig.AddForce (new Vector2 (0f, jumpForce));
finger3 = touch.fingerId;
}
}
Also, finding game object on each update is a performance degradation, like this:
grounded = GameObject.FindGameObjectWithTag ("player").GetComponent<kontrola> ().ground;
Instead capture the reference of game object in the start or from inspector, then try like:
grounded = playerGO.GetComponent<kontrola> ().ground;
Have set all the else if to ifs and still doesnt work. Grounded must be in update because other script looks if the object is grounded but i will move it to this script about that you are right it is too consumpting....
I will try reconfiguring the touchid logic i think i have missed some of it there.
Where resetting fingerID, set it to -1
ins$$anonymous$$d of 1,2,3.
Like:
if(touch.fingerId == finger1){
finger1 = -1;
movingleft = false;
}
if(touch.fingerId == finger2){
finger2 = -1;
movingright = false;
}
if(touch.fingerId == finger3){
finger3 = -1;
}
Isnt fingerId to store unique identifier for the touches that are stored in Input.touches array. That way i know that my touch was the right one to point it to the right statement.
I have noticed: when i press left i go left and then i press somewhere on the screen.. It interupts my touch and the gameobject stops. I think i have problem in looping the touches array. $$anonymous$$aybe is another way.
I have also written an else statement that returns nothing....
Still when i make another input touch into an array it interupts other touch.
So you have a script with four variables for four touches each, and with how many games objects this script is attached to ?
Answer by dadika4 · Aug 31, 2015 at 12:27 PM
I have soved the problem when input.gettouch(i) and touchphase begin or move must be in one loop. Touchphase. end must be in other loop
Than it works
Your answer
Follow this Question
Related Questions
Drawing 2 lines in same time 0 Answers
FingerID problem 1 Answer
Multitouch sometimes doesn't work properly when fingers are close together. 1 Answer