Author Topic: NGUI Virtual Joystick  (Read 22632 times)

JayToronto

  • Guest
NGUI Virtual Joystick
« on: April 16, 2012, 12:34:16 AM »
I was wondering if anyone's tried out the NGUI virtual joystick marjan posted here:

http://www.tasharen.com/?topic=an-ngui-virtual-joystick-solution

It's a .zip file instead of a .unitypackage file


If I unpack it it just looks like this:

I tried removing the .zip extension (leaving just .unitypackage) but unity says it's an unknown format.

Am I doing something wrong? Thanks in advance.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI Virtual Joystick
« Reply #1 on: April 16, 2012, 12:35:34 AM »
I just downloaded it, and that file contains a single unitypackage file inside, so it's something with your extracting application.

JayToronto

  • Guest
Re: NGUI Virtual Joystick
« Reply #2 on: April 16, 2012, 12:41:41 AM »
Thanks! In case anyone runs into the same problem, I was extracting it using OSX's default extractor. I managed to unzip it properly using "The Unarchiver" app.

JayToronto

  • Guest
Re: NGUI Virtual Joystick
« Reply #3 on: April 16, 2012, 12:53:02 AM »
It seems I ran into the same problem as JSorrentino. Since the old forums are closed, I'll quote him here, in case marjan wants to reply:

Quote
Couple questions, if this is being kept up on…

The changes posted above at line 142, is this incorporated in the zip package – it appears not to be, and if this is the case – does it replace everything from 142 (there’s a else if statement in the original) and you’re missing symbols in the code above (if (length radius)).

Getting Tweener and obsolete camera calls:

Assets/NGUI/Scripts/UIJoystick.cs(58,86): error CS0103: The name `Tweener’ does not exist in the current context

(Filename: Assets/NGUI/Scripts/UIJoystick.cs Line: 58)

Assets/NGUI/Scripts/UIJoystick.cs(87,52): warning CS0618: `UICamera.lastCamera’ is obsolete: `Use UICamera.currentCamera instead’

(Filename: Assets/NGUI/Scripts/UIJoystick.cs Line: 87)

Error: Analytics Event: 5(Compiler*error CS0103*The name `Tweener’ does not exist in the current context)(1): skipped because it was sent more than once in 0.10 seconds
Assets/NGUI/Scripts/UIJoystick.cs(95,113): error CS0103: The name `Tweener’ does not exist in the current context

(Filename: Assets/NGUI/Scripts/UIJoystick.cs Line: 95)

Assets/NGUI/Scripts/UIJoystick.cs(118,36): warning CS0618: `UICamera.lastCamera’ is obsolete: `Use UICamera.currentCamera instead’

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI Virtual Joystick
« Reply #4 on: April 16, 2012, 12:54:40 AM »
Tweener was renamed to UITweener.
UICamera.lastCamera is now UICamera.currentCamera.

ikars

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: NGUI Virtual Joystick
« Reply #5 on: May 21, 2012, 06:01:34 PM »
Trying to use this script as well. I updated the UITweener & currentCamera occurrences, but still get one that I can't decipher:

"Assets/Plugins/UIJoystick.cs(118,27):error CS0103: The name 'UICamera' does not exist in the current context"

the error points to this line of code:

Ray ray = UICamera.currentCamera.ScreenPointToRay(UICamera.lastTouchPosition);

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI Virtual Joystick
« Reply #6 on: May 21, 2012, 07:22:49 PM »
UICamera doesn't exist? UICamera is an integral part of NGUI. If it doesn't exist, then your UI won't receive any events.

ikars

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: NGUI Virtual Joystick
« Reply #7 on: May 21, 2012, 07:42:37 PM »
Well, I don't think the error message is particularly helpful in this instance. The entire scene works fine -- including various NGUI buttons -- as long as the UIJoystick script is not present. The minute I include it, I get the UICamera does not exist error...

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI Virtual Joystick
« Reply #8 on: May 21, 2012, 07:44:00 PM »
Ah, because the script looking for it is inside the Plugins folder.

NGUI is outside it by default. Move that script out of Plugins.

ikars

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 6
    • View Profile
Re: NGUI Virtual Joystick
« Reply #9 on: May 21, 2012, 07:52:13 PM »
Bingo! Thank you, sir :)

Somewhat new to Unity -- thought it might be a path issue.
Is the 'plugins' folder a special case? The script seems happy
anywhere else.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,154
  • Toronto, Canada
    • View Profile
Re: NGUI Virtual Joystick
« Reply #10 on: May 21, 2012, 07:56:43 PM »
Unity compiles everything in Plugins and Standard Assets folder prior to everything else. Since NGUI is outside of it, it's simply not visible from inside Plugins.

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: NGUI Virtual Joystick
« Reply #11 on: May 31, 2012, 10:33:50 AM »
I've been using this joystick with NGUI, it's mostly going well but if normalise is enabled I don't get any reading for the x & y position of the joystick. If I disable normalise I get a reading of -0.49 to 0.49 but it's only giving that position when the joystick is almost fully left/right/up/down it's as though there's a deadzone but I've set that to zero.

Any ideas?  :)

Could it be because I had to scale the joysticks down?

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: NGUI Virtual Joystick
« Reply #12 on: May 31, 2012, 12:15:29 PM »
I've figured out the scaling issue, it was because of a lack of UIRoot on the root object however the joysticks still require being almost fully in any direction to give a reading on the x or y position. The deadzone is 0, i've also tried -2 and such but it's the same.  :(

Any ideas?

n8

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 23
    • View Profile
Re: NGUI Virtual Joystick
« Reply #13 on: June 04, 2012, 04:52:55 PM »
I've figured out the scaling issue, it was because of a lack of UIRoot on the root object however the joysticks still require being almost fully in any direction to give a reading on the x or y position. The deadzone is 0, i've also tried -2 and such but it's the same.  :(

Any ideas?

So I started messing around with this package and ran into the same issues as everybody else.  I believe that I have it working now.  below is the entire "OnDrag" function from the UIJoystick.cs file.  the key is the "if (length > radius)" part.  that is really all there is that is different from the previous.  with this code, I am seeing good range from 0 - 1 without the crazy jump.  Hope this helps

  1. void OnDrag (Vector2 delta)
  2.         {
  3.                 //Debug.Log("delta " +  delta + " delta.magnitude = " + delta.magnitude);
  4.                 Ray ray = UICamera.lastCamera.ScreenPointToRay(UICamera.lastTouchPosition);
  5.                 float dist = 0f;
  6.                
  7.                 Vector3 currentPos = ray.GetPoint(dist);
  8.                 Vector3 offset = currentPos - userInitTouchPos;
  9.  
  10.                 if (offset.x != 0f || offset.y != 0f) {
  11.                         offset = target.InverseTransformDirection(offset);
  12.                         offset.Scale(scale);
  13.                         offset = target.TransformDirection(offset);
  14.                         offset.z = 0f;
  15.                 }
  16.                
  17.                 target.position = userInitTouchPos +offset;
  18.  
  19.                 Vector3 zeroZpos = target.position;
  20.                 zeroZpos.z = 0f;
  21.                 target.position = zeroZpos;
  22.                 // Calculate the length. This involves a squareroot operation,
  23.                 // so it's slightly expensive. We re-use this length for multiple
  24.                 // things below to avoid doing the square-root more than one.
  25.  
  26.                 float length = target.localPosition.magnitude;
  27.                
  28.                 if (length > radius) {
  29.                         target.localPosition = Vector3.ClampMagnitude(target.localPosition, radius);
  30.                 }
  31.                 position = target.localPosition;
  32.                
  33.                 if (normalize) {
  34.                         // Normalize the vector and multiply it with the length adjusted
  35.                         // to compensate for the deadZone radius.
  36.                         // This prevents the position from snapping from zero to the deadZone radius.
  37.                         position = position / radius * Mathf.InverseLerp (radius, deadZone, 1);
  38.                 }
  39.         }
  40.  

derkoi

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 57
    • View Profile
Re: NGUI Virtual Joystick
« Reply #14 on: June 05, 2012, 01:32:58 AM »
Thanks, that works great.  ;D