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 Matthew Scott · Mar 21, 2014 at 01:47 PM · vector3randomfloatconvertseed

Vector 3 to float eg V3(1, 2, 3) -> float 123 in JS?

Hi everyone,

We are doing some programming where we need to generate a float seed based of a vector 3 value, is there any efficient/simple way of taking a Vector3 such as Vector3(1, 2, 3) and turning it into a float 123?

Comment
Add comment · Show 3
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 Dblfstr · Mar 21, 2014 at 02:30 PM 1
Share

Similar to @YourGamesBeOver 's answer: Access each x,y,z value using this[int]; where [int] is an index 0,1,2, for x,y,z.

using UnityEngine; using System.Collections;

 public class Example : $$anonymous$$onoBehaviour {
     public Vector3 seedVector = new Vector3(1,2,3);
     void Example() {
        float seed = (seedVector[0]*100) + (seedVector[1]*10) + (seedVector[2]);
     }

}

Another alternative is to create a string from the vector using ToString(), then converting the string to a float... but I don't know if that is good coding practice.

avatar image Dblfstr Dblfstr · Mar 21, 2014 at 02:31 PM 0
Share

converting to a comment.

avatar image Matthew Scott · Mar 21, 2014 at 03:17 PM 0
Share

Well, it's almost what I needed...but this will work just as well!

In theory bitshifting would have worked better, but, it's not needed for what I require:

Vector3(100, 20, 30) -> float : 1002030

Vector3(100 100, 20 10, 30) = Vector3(10000, 200, 30) = 10,230

It's close, but it's perfect for what I need, even a vector with the same values in a different order will produce different results:

Vector3(20 100 (+), 30 10 (+), 100) = 2400;

Thanks guys!

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by YourGamesBeOver · Mar 21, 2014 at 02:17 PM

there are numerous solutions: If the components of the vector are single digits, you could multiply them by 100,10,1 and then add them together:

seed = vector.x*100+vector.y*10+vector.z;

Actually, that solution works for any number of digits, you could just change the scalars

If you don't actually care about what the final value is, you could use bitshifting.
I'm not too sure of the syntax in C# or js (most of my knowledge is in Java), but it'd be something like this: seed = vector.x<<16|vector.y<<8|vector.z; derp... I just remembered you can't bitshift floats (at least in c#), you could make them ints first...

Hopefully if I haven't answered your question, I at least gave you some ideas!

Comment
Add comment · 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

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

22 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

Related Questions

Using a variable as a components for Vector3 1 Answer

convert object to float 2 Answers

How do I generate persistant float values from a random seed? 3 Answers

Unity 2D How can I detect collision with a Vector3? 2 Answers

How to convert 00:40:00.0 to float 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