- Home /
MultiTouch issues
I am having 4 textures as player controls in my scene on which I am trying to detect touches using the following code:
//left,right,attack,jump are textures;
for (int i = 0; i< Input.touches.GetLength(0); i++) {
if (left.HitTest (Input.GetTouch (i).position) && Input.GetTouch (i).phase != TouchPhase.Ended) {
//player.SendMessage ("Move", -1);
}
if (right.HitTest (Input.GetTouch (i).position) && Input.GetTouch (i).phase != TouchPhase.Ended) {
//player.SendMessage ("Move", 1);
}
if (!right.HitTest (Input.GetTouch (i).position) && !left.HitTest (Input.GetTouch (i).position)) {
//player.SendMessage ("Move", 0.0f);
}
if (jump.HitTest (Input.GetTouch (i).position) && Input.GetTouch (i).phase == TouchPhase.Began) {
//player.SendMessage ("Jump", true);
}
if (attack.HitTest (Input.GetTouch (i).position)) {
//player.SendMessage ("AttackKeyPressed", true);
}
}
The problem I am getting is that I am unable to detect simultaneous touches on 2 textures like left and jump texture. touching the jump textures stops the left texture action. How can I do this?
Or is there any better strategy to implement this?
if (!right.HitTest (Input.GetTouch (i).position) && !left.HitTest (Input.GetTouch (i).position)) {
//player.Send$$anonymous$$essage ("$$anonymous$$ove", 0.0f);
}
What's the purpose for this?
$$anonymous$$ul$$anonymous$$ch doesn't seem to be your problem. Does your movement model work with key presses?
of course movement won't work with the lines commented out. I was trying something else, that is why they are commented.
Answer by koray1396 · Apr 24, 2014 at 03:38 PM
SendMessage is not a good way to handle this, instead you may want to access player's script. Untested but something like below.
Also I have added a condition where both left & right buttons are pressed, in your script, it would go right.
public GameObject player;
PlayerScript someVariableName;
void Start(){
someVariableName = player.GetComponent<PlayerScript>();
}
void Update(){
if(Input.touchCount > 0){
for (int i = 0; i< Input.touchCount; i++) {
if(right.HitTest (Input.GetTouch(i).position) && left.HitTest (Input.GetTouch (i).position)){
someVariableName.Move(0.0f);
} else if (left.HitTest (Input.GetTouch(i).position) && Input.GetTouch (i).phase != TouchPhase.Ended) {
someVariableName.Move(-1.0f);
} else if (right.HitTest (Input.GetTouch(i).position) && Input.GetTouch (i).phase != TouchPhase.Ended) {
someVariableName.Move(1.0f);
} else if (!right.HitTest (Input.GetTouch(i).position) && !left.HitTest (Input.GetTouch (i).position)) {
someVariableName.Move(0.0f);
}
if (jump.HitTest (Input.GetTouch(i).position) && Input.GetTouch (i).phase == TouchPhase.Began) {
someVariableName.Jump(true);
}
if (attack.HitTest (Input.GetTouch(i).position)) {
someVariableName.AttackKeyPressed(true);
}
}
}
}
I've been meaning to ask this, but can you explain why Send$$anonymous$$essage is not a good way to handle this?? And i think you are missing a for loop in the above code
yes careless, fixed it now.
I'm not a Unity expert, maybe someone else can give you a much better explanation. But Send$$anonymous$$essage is a longer process, you search for methods by strings, which is unreliable and error prone. Also there were some posts about the performance of this vs GetComponent etc..., proving that Send$$anonymous$$essage is way much slower on tests(about hundreds time slower).
Although there are times that Send$$anonymous$$essage is the only reasonable option, but in your situation, it's not really necessary.
You can also put these directly into your player's script, and test if a GUITexture is touched, another script is not really necessary. You just need to define GUITexture objects.
Hope this helps.