Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by goodguy · Feb 18, 2018 at 04:51 AM · unity 5mathvector2tostring

Vector2 ToString and vector components show totally different values

I'm making scripts for vehicle control with virtual joysticks. Everything seemed to be working fine until I found a very strange bug which I just can't explain. So, I have a normalized Vector2 which is controlled by a joystick and is used to set direction of the vehicles, nothing special. But when this vector is pointing left a very weird thing happens, Vector2.ToString shows (1.0, 0.0) but Vector2.y shows 8.742278E-08 or -8.742278E-08 like this:

 void Update() {
     print("JOYSTICK INPUT" + inputVector + " INPUT Y " + inputVector.y); 
 }

And the output is

JOYSTICK INPUT(-1.0, 0.0) INPUT Y 8.742278E-08 UnityEngine.MonoBehaviour:print(Object) Joystick:Update() (at Assets/Virtual Joystick Pack/Scripts/Base/Joystick.cs:35)

I could explain it by changing value somewhere else if I would be getting it asynchronously but I just have no explanation of how can they be so different in synchronous output

I'm not a newbie in programming, I have many years of experience with it, but this bug really made me come here and ask for help

p.s. Here's the complete code of my custom Joystick and its base class (base asset can be found here https://assetstore.unity.com/packages/tools/input-management/joystick-pack-107631). The base joystick has the same problem

    using UnityEngine;
     using UnityEngine.EventSystems;
     public class Joystick : MonoBehaviour, IDragHandler, IPointerUpHandler, IPointerDownHandler
     {
         [Header("Options")]
         [Range(0f, 2f)] public float handleLimit = 1f;
     
         [HideInInspector]
         public Vector2 inputVector = Vector2.zero;
     
         [Header("Components")]
         public RectTransform background;
         public RectTransform handle;
     
         public virtual float Horizontal { get { return inputVector.x; } }
         public virtual float Vertical { get { return inputVector.y; } }
     
     
         public virtual void OnDrag(PointerEventData eventData)
         {
     
         }
     
         public virtual void OnPointerDown(PointerEventData eventData)
         {
     
         }
     
         public virtual void OnPointerUp(PointerEventData eventData)
         {
     
         }
         void Update() {
             print("JOYSTICK IN PUT" + inputVector + " INPUT Y " + inputVector.y);
         }
     }
 

And my joystick

 using UnityEngine;
 using UnityEngine.EventSystems;
 
 public class SnapableFixedJoystick : Joystick
 {
     [Header("Snappable Fixed Joystick")]
     
     Vector2 joystickPosition = Vector2.zero;
     private Camera cam = new Camera();
     private const int _snapAngle                                    = 45;        
     private float _halfSnapAngle                                    = 0.0f;    
 
     void Start()
     {
          _halfSnapAngle  = _snapAngle / 2;
         joystickPosition = RectTransformUtility.WorldToScreenPoint(cam, background.position);
     }
 
     public override void OnDrag(PointerEventData eventData)
     {
         Vector2 direction = eventData.position - joystickPosition;
         
         float degAngle        = Mathf.Atan2(direction.y, direction.x) * Mathf.Rad2Deg;
         if (_snapAngle > 0) {
             degAngle += _halfSnapAngle;
             if (degAngle >= 0) {
                 degAngle -= (degAngle % _snapAngle);
             } else {
                 degAngle -= (_snapAngle + (degAngle % _snapAngle));
             }
             // used to snap joystick handle to a specific angle
             SetVectorAngle(ref direction, degAngle * Mathf.Deg2Rad);
         }
 
         inputVector = (direction.magnitude > background.sizeDelta.x / 2f) ? direction.normalized : direction / (background.sizeDelta.x / 2f);
         handle.anchoredPosition = (inputVector * background.sizeDelta.x / 2f) * handleLimit;
     }
     private void SetVectorAngle(ref Vector2 vector, float angle) {
         var len        = vector.magnitude;
         vector.x    = Mathf.Cos(angle) * len;
         vector.y    = Mathf.Sin(angle) * len;
     }
 
     public override void OnPointerDown(PointerEventData eventData)
     {
         OnDrag(eventData);
     }
 
     public override void OnPointerUp(PointerEventData eventData)
     {
         inputVector = Vector2.zero;
         handle.anchoredPosition = Vector2.zero;
     }
 }



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
1
Best Answer

Answer by Bunny83 · Feb 18, 2018 at 05:43 AM

They are not totally different values. You should learn to read scientific notation. By default ToString of Unity's vector structs will round each component to 1 digit behind the decimal point. When you print the components manually you basically call ToString on a float value which be default shows much more digits and uses the default scientific notation.


The value 8.742278E-08 is just 8.742278 * 10^-08. So the actual value in decimal notation is 0.00000008742278 which gets of course rounded to just "0.0" when you round to only one decimal place.


You may also want to watch the Numberphile video on floating point numbers

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 goodguy · Feb 18, 2018 at 06:09 AM 0
Share

Damn! You're totally right! I should have postponed this until morning :) Thank you so much!

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.

Follow this Question

Answers Answers and Comments

209 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 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 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 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 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 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 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 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

Turn a vector3 relative to a look direction 1 Answer

My movement script is not moving my 2D object 2 Answers

Vector2 intersection 0 Answers

[noob] vector2 cuestions 0 Answers

Character move speed not working 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