- Home /
Multi Touch GUI buttons
I got this script, i tested it with my tablet, and it worked. But how will i be able to press 2 GUI.Buttons at the same time? (like run and jump at same time)
Here's what i got:
public Texture2D leftIcon;
public Texture2D rightIcon;
public Texture2D jumpIcon;
public GUIStyle framestyle;
void OnGUI () {
if(GUI.RepeatButton(new Rect(200,Screen.height - 200,200,200), rightIcon, framestyle)){
float h = 1;
if(h * rigidbody2D.velocity.x < maxSpeed){
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
}
//Debug.Log ("velocity.x :" + rigidbody2D.velocity.x);
// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();
}
if(GUI.RepeatButton(new Rect (0,Screen.height - 200,200,200), leftIcon, framestyle)) {
float h = -1;
if(h * rigidbody2D.velocity.x < maxSpeed){
// ... add a force to the player.
rigidbody2D.AddForce(Vector2.right * h * moveForce);
}
//Debug.Log ("velocity.x :" + rigidbody2D.velocity.x);
// If the player's horizontal velocity is greater than the maxSpeed...
if(Mathf.Abs(rigidbody2D.velocity.x) > maxSpeed)
// ... set the player's velocity to the maxSpeed in the x axis.
rigidbody2D.velocity = new Vector2(Mathf.Sign(rigidbody2D.velocity.x) * maxSpeed, rigidbody2D.velocity.y);
// If the input is moving the player right and the player is facing left...
if(h > 0 && !facingRight)
// ... flip the player.
Flip();
// Otherwise if the input is moving the player left and the player is facing right...
else if(h < 0 && facingRight)
// ... flip the player.
Flip();
}
if(GUI.Button (new Rect(Screen.width - 200,Screen.height - 200,200,200), jumpIcon, framestyle) && grounded){
jump=true;
}
}
}
(Its a change of the Unity2D controller script)
"(Its a change of the Unity2D controller script)"
Can you tell me where exatly is that script, I'm trying to find it but I don't know where is it :(
Thank you :)
Answer by fafase · Nov 29, 2013 at 07:48 AM
You would have to use a different approach.
Each button is within a rectangle, you already have them you just need to store them in a Rect, so you can check for Contains.
Rect rightIconRect = new new Rect(200,Screen.height - 200,200,200);
Rect leftIconRect = new Rect (0,Screen.height - 200,200,200);
foreach(Touch t in Input.touches)
{
Vector2 vec = t.position;
vec.y = Screen.height - vec.y; // You need to invert since GUI and screen have differnet coordinate system
if(rightIconRect.Contains(vec))// Do something
if(leftIconRect.Contains(vec)) // Do something
}
Also, I think some touches are actually really close, it could return two touches from the same finger, so you may want to make sure you are calling performing twice an action from the same finger just because you have multiple touches from it.
I have absolute no clue where to place this.. But thanks for the help :)
O$$anonymous$$....how to add texture (or at least text) to the rect that we have created....??
Use some GUITexture. Good point, it is more efficient than GUI.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Increase Jump Through Mouse Clicks? 1 Answer
A node in a childnode? 1 Answer
Switching Cameras at runtime 1 Answer
2D Game - Y axis rotation (left - right) 2 Answers