Reputation: 1035
I have 2 gui textures.
According to screen width and height i kept it in gui.
One for joystick and another for shooter.
Now touching on shooter joystick moves to that specific portion.
i used rect.Contains.
void Start () {
xx = Screen.width - Screen.width/12;
yy = Screen.height - Screen.height/8;
lb = Screen.width/10;
rect = new Rect(-xx/2, -yy/2, lb, lb);
shooter.pixelInset = rect;
shooter.enabled = false;
}
void OnGUI(){
if(characterScript.playbool){
shooter.enabled = true;
}
if (rect.Contains(Event.current.mousePosition)){
shootBool = true;
print("shoot");
alert.text="shoot";
}
}
Not working properly for me. Think space coordinates are different from gui coordinates. How can fix this problem.do anyone can suggest any other good method
Upvotes: 0
Views: 4467
Reputation: 6132
You can try HitTest
.
function Update()
{
for (var touch : Touch in Input.touches)
{
if (rect.Contains(touch.position))
{
// we are now in the guitexture 'rect'
Debug.Log("rect touched");
exit;
}
}
}
The above code is used with touches, as you described in your question. However, since you tagged mouse
, I don't know for sure if you use the mouse or a touch.
So, if you use the mouse to click on the object, you can use:
if (rect.Contains(Input.mousePosition) && Input.GetMouseButtonDown(0))
{
Debug.Log("rect clicked");
exit;
}
Upvotes: 1
Reputation: 155
Some debugging of your code brings out a few problems.
Say the screen resolution is 800x600px:
xx = 800 - 800 / 12
= 733.3333333333333
yy = 600 - 600 / 8
= 525
lb = 800 / 10
= 80
rect = (-366.665, -262.5, 80, 80)
Why is lb
determined by the screen width divided by ten?
Now the heart of the problem is that Event.mousePosition
starts at the top left of the screen, while GUITexture.pixelInset
is based in the center of the screen.
You will have to either adjust the Event.mousePosition
to use the center of the screen as the origin, or you will have to adjust the rect
variable to start at the top-left of the screen.
Upvotes: 0