Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Nanako · Nov 17, 2014 at 01:23 AM · floatvector2newinitializationdeclare

How do i declare a negative float while initializing a vector?

This should be basic, baby stuff.

Here's a little snippet of my code:

 public Vector2 SubjectScreenOffset = new Vector2(-0.125f, 0.0f);//offset from the centre of the screen. a value of +-0.5 will reach the edge of the appropriate axis
 
     void CalculateCameraOffset()
     {
         Vector2 screenSize = new Vector2(cam.pixelWidth, cam.pixelHeight);
         print("SubjectScreenOffset = " + SubjectScreenOffset);
         print("ScreenSize = " + screenSize);
         print("Testvalue: " + (SubjectScreenOffset.x * screenSize.x));
         Vector2 screenPixelPosition = new Vector2((SubjectScreenOffset.x * screenSize.x) + (screenSize.x * 0.5f), (SubjectScreenOffset.y * screenSize.y) + (screenSize.y * 0.5f));
         print("ScreenPixelPosition = " + screenPixelPosition);
         //Vector3 desiredPlayerPos = cam.ScreenToWorldPoint();
     }



and the first print results: "SubjectScreenOffset = (0.1, 0.0)"

Clearly that is not what i declared as the initial value. Two decimal places have been chopped off, and the sign is lost. What's going on ?

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 · Nov 17, 2014 at 01:45 AM

They haven't been chopped off. It's just the Vector2/3's ToString method that rounds the values to 1 decimal digit since the string representation of 3 floats would else be quite large.

The value also will keep it's sign and it's also printing for me. You either haven't seen it or you do something different from what you showed here. Keep in mind that your SubjectScreenOffset variable is a public variable, so the value you set in the field initializer is only relevant when you attach the script to the gameobject. From now on the value is serialized and can be changed in the inspector which you might have done. So check the value in the inspector.

Comment
Add comment · Show 2 · 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 Nanako · Nov 17, 2014 at 01:50 AM 0
Share

ah this was the issue, i'd set it in the inspector at some point yesterday and forgotten. thank you

avatar image Kiwasi · Nov 17, 2014 at 03:25 AM 0
Share

Nice work. Didn't think of that one, but its caught me out before.

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

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

Neural Network type conversion problem 1 Answer

Why do these variables work without declaring type? (JS) 2 Answers

Modify a float value 1 Answer

Vector doesn't get calculated 1 Answer

UnityEngine.Vector2 Float conversion problem 2 Answers


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