• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheShadyColombian · Oct 22, 2014 at 01:31 AM · c#inspectorvector2penelope

Joystick position not appearing in inspector

I am using the C# translation of the Penelope joystick script, but the public vector2 position does not appear in the inspector. I will try accessing it from another script and see if the script can read it (which is my purpose). here is the script:

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 
 public class Boundary
 {
         public Vector2 min = Vector2.zero;
         public Vector2 max = Vector2.zero;
 }
 
 
 [RequireComponent(typeof(GUITexture))]
 public class Joystick : MonoBehaviour
 {
 
         public float JoystickPositionX;
         public float JoystickPositionY;
 
         /* Is this joystick a TouchPad? */
         public bool touchPad = false;
         /* In case the joystick is a touchPad, should its GUI be faded when inactive? */
         public bool fadeGUI = false;
         /* Control when position is output */
         public Vector2 deadZone = Vector2.zero;
         /* Normalize output after the dead-zone? */
         public bool normalize = false;
         /* Current tap count */
         public int tapCount = -1;
 
         /* The touchZone of the joystick */
         public Rect touchZone;
 
         /* Finger last used on this joystick */
         public int lastFingerId = -1;
         /* How much time there is left for a tap to occur */
         public float tapTimeWindow;
         public Vector2 fingerDownPos;
         /*
      * Currently unused.
     public float fingerDownTime;
     */
         public float firstDeltaTime;
 
         /* Joystick graphic */
         private GUITexture gui;
         /* Default position / extents of the joystick graphic */
         private Rect defaultRect;
         /* Boundary for joystick graphic */
         private Boundary guiBoundary = new Boundary();
         /* Offset to apply to touch input */
         private Vector2 guiTouchOffset;
         /* Center of joystick */
         private Vector2 guiCenter;
 
         private bool isFingerDown
         {
                 get
                 {
                         return (lastFingerId != -1);
                 }
         }
 
         public int latchedFinger
         {
                 set
                 {
                         /* If another joystick has latched this finger, then we must release it */
                         if (lastFingerId == value)
                         {
                                 Restart();
                         }
                 }
         }
 
         /* The position of the joystick on the screen ([-1, 1] in x,y) for clients to read. */
         public Vector2 JoystickPosition {
                 get;
                 set;
         }
 
         public Vector2 position
         {
                 get;
                 private set;
         }
 
 
         public static string joysticksTag = "joystick";
         /* A static collection of all joysticks */
         public static List<Joystick> joysticks;
         /* Has the joysticks collection been enumerated yet? */
         public static bool enumeratedJoysticks = false;
         /* Time allowed between taps */
         public static float tapTimeDelta = 0.3f;
 
 
         public void Reset()
         {
                 try
                 {
                         gameObject.tag = joysticksTag;
                 }
                 catch (Exception)
                 {
                         Debug.LogError("The \""+joysticksTag+"\" tag has not yet been defined in the Tag Manager.");
                         throw;
                 }
         }
 
 
         public void Awake()
         {
                 gui = GetComponent<GUITexture>();
                 if (gui.texture == null)
                 {
                         Debug.LogError("Joystick object requires a valid texture!");
                         gameObject.active = false;
                         return;
                 }
 
                 if (!enumeratedJoysticks)
                 {
                         try
                         {
                                 /* Collect all joysticks in the game, so we can relay finger latching messages */
                                 GameObject[] objs = GameObject.FindGameObjectsWithTag(joysticksTag);
                                 joysticks = new List<Joystick>(objs.Length);
                                 foreach (GameObject obj in objs)
                                 {
                                         Joystick newJoystick = obj.GetComponent<Joystick>();
                                         if (newJoystick == null)
                                         {
                                                 throw new NullReferenceException("Joystick gameObject found without a suitable Joystick component.");
                                         }
                                         joysticks.Add(newJoystick);
                                 }
                                 enumeratedJoysticks = true;
                         }
                         catch (Exception exp)
                         {
                                 Debug.LogError("Error collecting Joystick objects: "+exp.Message);
                                 throw;
                         }
                 }
 
                 /* Store the default rect for the gui, so we can snap back to it */
                 defaultRect = gui.pixelInset;
 
                 defaultRect.x += transform.position.x * Screen.width;// + gui.pixelInset.x; // -  Screen.width * 0.5f;
                 defaultRect.y += transform.position.y * Screen.height;// - Screen.height * 0.5f;
 
                 transform.position = new Vector3(0, 0, transform.position.z);
 
                 if (touchPad)
                 {
                         /* Use the rect from the gui as our touchZone */
                         touchZone = defaultRect;
                 }
                 else
                 {
                         /* This is an offset for touch input to match with the top left corner of the GUI */
                         guiTouchOffset.x = defaultRect.width * 0.5f;
                         guiTouchOffset.y = defaultRect.height * 0.5f;
 
                         /* Cache the center of the GUI, since it doesn't change */
                         guiCenter.x = defaultRect.x + guiTouchOffset.x;
                         guiCenter.y = defaultRect.y + guiTouchOffset.y;
 
                         /* Let's build the GUI boundary, so we can clamp joystick movement */
                         guiBoundary.min.x = defaultRect.x - guiTouchOffset.x;
                         guiBoundary.max.x = defaultRect.x + guiTouchOffset.x;
                         guiBoundary.min.y = defaultRect.y - guiTouchOffset.y;
                         guiBoundary.max.y = defaultRect.y + guiTouchOffset.y;
                 }
 
         }
 
 
         public void Enable()
         {
                 enabled = true;
         }
 
 
         public void Disable()
         {
                 enabled = false;
         }
 
 
         public void Restart()
         {
                 /* Release the finger control and set the joystick back to the default position */
                 gui.pixelInset = defaultRect;
                 lastFingerId = -1;
                 position = Vector2.zero;
                 fingerDownPos = Vector2.zero;
 
                 if (touchPad && fadeGUI)
                 {
                         gui.color = new Color(gui.color.r, gui.color.g, gui.color.b, 0.025f);
                 }
         }
 
 
         public void Update()
         {
 
                 int count = Input.touchCount;
 
                 /* Adjust the tap time window while it still available */
                 if (tapTimeWindow > 0)
                 {
                         tapTimeWindow -= Time.deltaTime;
                 }
                 else
                 {
                         tapCount = 0;
                 }
 
                 if (count == 0)
                 {
                         Restart();
                 }
                 else
                 {
                         for (int i = 0; i < count; i++)
                         {
                                 Touch touch = Input.GetTouch(i);
                                 Vector2 guiTouchPos = touch.position - guiTouchOffset;
 
                                 bool shouldLatchFinger = false;
                                 if (touchPad && touchZone.Contains(touch.position))
                                 {
                                         shouldLatchFinger = true;
                                 }
                                 else if (gui.HitTest(touch.position))
                                 {
                                         shouldLatchFinger = true;
                                 }
 
                                 /* Latch the finger if this is a new touch */
                                 if (shouldLatchFinger && (lastFingerId == -1 || lastFingerId != touch.fingerId))
                                 {
 
                                         if (touchPad)
                                         {
                                                 if (fadeGUI)
                                                 {
                                                         gui.color = new Color(gui.color.r, gui.color.g, gui.color.b, 0.15f);
                                                 }
                                                 lastFingerId = touch.fingerId;
                                                 fingerDownPos = touch.position;
                                                 /*
                          * Currently unused.
                         fingerDownTime = Time.time;
                         */
                                         }
 
                                         lastFingerId = touch.fingerId;
 
                                         /* Accumulate taps if it is within the time window */
                                         if (tapTimeWindow > 0)
                                         {
                                                 tapCount++;
                                         }
                                         else
                                         {
                                                 tapCount = 1;
                                                 tapTimeWindow = tapTimeDelta;
                                         }
 
                                         /* Tell other joysticks we've latched this finger */
                                         foreach (Joystick j in joysticks)
                                         {
                                                 if (j == this)
                                                 {
                                                         continue;
                                                 }
                                                 j.latchedFinger = touch.fingerId;
                                         }
                                 }
 
                                 if (lastFingerId == touch.fingerId)
                                 {
                                         /*
                         Override the tap count with what the iOS SDK reports if it is greater.
                         This is a workaround, since the iOS SDK does not currently track taps
                         for multiple touches.
                     */
                                         if (touch.tapCount > tapCount)
                                         {
                                                 tapCount = touch.tapCount;
                                         }
 
                                         if (touchPad)
                                         {
                                                 /* For a touchpad, let's just set the position directly based on distance from initial touchdown */
                                                 position = new Vector2
                                                         (
                                                                 Mathf.Clamp((touch.position.x - fingerDownPos.x) / (touchZone.width / 2), -1, 1),
                                                                 Mathf.Clamp((touch.position.y - fingerDownPos.y) / (touchZone.height / 2), -1, 1)
                                                         );
                                         }
                                         else
                                         {
                                                 /* Change the location of the joystick graphic to match where the touch is */
                                                 gui.pixelInset = new Rect
                                                         (
                                                                 Mathf.Clamp(guiTouchPos.x, guiBoundary.min.x, guiBoundary.max.x),
                                                                 Mathf.Clamp(guiTouchPos.y, guiBoundary.min.y, guiBoundary.max.y),
                                                                 gui.pixelInset.width,
                                                                 gui.pixelInset.height
                                                         );
                                         }
 
                                         if (touch.phase == TouchPhase.Ended || touch.phase == TouchPhase.Canceled)
                                         {
                                                 Restart();
                                         }
                                 }
                         }
 
                 }
 
                 if (!touchPad)
                 {
                         /* Get a value between -1 and 1 based on the joystick graphic location */
                         position = new Vector2
                                 (
                                         (gui.pixelInset.x + guiTouchOffset.x - guiCenter.x) / guiTouchOffset.x,
                                         (gui.pixelInset.y + guiTouchOffset.y - guiCenter.y) / guiTouchOffset.y
                                 );
                 }
 
                 /* Adjust for dead zone */
                 float absoluteX = Mathf.Abs(position.x);
                 float absoluteY = Mathf.Abs(position.y);
 
                 if (absoluteX < deadZone.x)
                 {
                         /* Report the joystick as being at the center if it is within the dead zone */
                         position = new Vector2(0, position.y);
                 }
                 else if (normalize)
                 {
                         /* Rescale the output after taking the dead zone into account */
                         position = new Vector2(Mathf.Sign(position.x) * (absoluteX - deadZone.x) / (1 - deadZone.x), position.y);
                 }
 
                 if (absoluteY < deadZone.y)
                 {
                         /* Report the joystick as being at the center if it is within the dead zone */
                         position = new Vector2(position.x, 0);
                 }
                 else if (normalize)
                 {
                         /* Rescale the output after taking the dead zone into account */
                         position = new Vector2(position.x, Mathf.Sign(position.y) * (absoluteY - deadZone.y) / (1 - deadZone.y));
                 }
         }
 
 
 }


Other scripts CAN access the variable position, but it is not displayed in the inspector. also, if i make the variable public in a different script, it will display it!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by bubzy · Oct 22, 2014 at 06:11 AM

do you have the GUITexture that your script requires in the scene?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheShadyColombian · Oct 22, 2014 at 02:08 PM 0
Share

it displays the joystick, but not the position vector2 variable in the inspector

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

28 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Random.Range issue 1 Answer

Modifying default Inspector GUI creator 3 Answers

Show custom List in Inspector? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges