I’m just starting with touch in unity and I am very confused over one issue, I have made an area on screen make the character jump when touched, so I then tried to draw a button over this area with the GUI, and they came out in different places and different sizes.
Here’s the code I’m using, where jumpButtonPosMin and Max are 50 and 100 respectively:
function CheckTouch () {
for (var i = 0; i < Input.touchCount; ++i) {
touch = Input.GetTouch(i);
var touchPos : Vector2 = touch.position;
if (touchPos.x > jumpButtonPosMin.x && touchPos.x < jumpButtonPosMax.x)
{
if (touchPos.y > jumpButtonPosMin.y && touchPos.y < jumpButtonPosMax.y)
{
Jump();
}
}
}
}
function OnGUI() {
GUI.DrawTexture(Rect(50,50,100,100), jumpButton, ScaleMode.ScaleToFit, true, 0);
}
The result is this:
______________________
| |
| Button |
| |
| |
| Touch |
______________________