Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: psavi on April 29, 2015, 11:54:34 AM

Title: Touch On Object (TouchJoystick)
Post by: psavi on April 29, 2015, 11:54:34 AM
Hello!

I am trying to do a virtual joystick and i was using a script in my camera(it was working well) but now i want to implement the NGUI interface.


This is what i was using :
  1.                
  2.                                 RaycastHit2D hit = Physics2D.Raycast(Camera.main.ScreenToWorldPoint(Input.GetTouch(i).position),Vector2.zero);
  3.                        
  4.                                 if(hit.collider != null) {
  5.  
  6.                                 if (hit.collider.name == "Button_Left")
  7.                                 {
  8.  
  9.                                 //the character move to left
  10.                                 }
  11.                                 ..........
  12.        

 I want to know how can i verify if a touch position is colliding a specific object. I have tested in many different ways using the "UIEventListener". The "On Hover Over" option doesn't work well because when i click out of the collider and snap the touch position into the collider it dont trigger. The "On Hover Over" option only trigger when i click first inside the collider area.

I have trying in diferent ways, combining OnPress, OnDrag, Hover.... But dont work as i want. I only want to know the information "if (have a touch position on the button)".

Buttons that use "OnPress" to trigger, like "Jump Event" worked well, but the directional buttons dont...

(http://s17.postimg.org/mnk26am2n/Screen_Shot_2015_04_29_at_13_28_42.png)

 If anyone understand what i want to do and make it clear for me, i would be grateful!
Title: Re: Touch On Object (TouchJoystick)
Post by: ArenMook on April 29, 2015, 09:17:14 PM
I don't understand what the code you pasted is supposed to do. Why are you doing your own raycasts? Each touch results in a raycast by NGUI, which results in an appropriate NGUI event such as OnPress or OnDrag. UICamera.currenTouch gives you access to detailed information about this touch. UICamera.lastTouchPosition gives you the position. There is even world position if you need that.

To implement joystick navigation, check the example that comes with NGUI. It's done using UIKeyNavigation.
Title: Re: Touch On Object (TouchJoystick)
Post by: yo_milo on April 30, 2015, 11:32:24 AM
OnPress only triggers when something is pressed or unpressed.
  1. void OnPress (bool isPressed)
  2. {
  3.     if (isPressed) Debug.Log("I was pressed on!");
  4.     else Debug.Log("I was unpressed");
  5. }
  6.  

Thus, you could modify a variable in your script that changes, depending if isPressed is true or false.