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
1
Question by Rush3fan · Mar 24, 2013 at 12:09 AM · scalevectorconvertnormal

How do I convert a normal to a scale?

So, say I had a normalized direction and wanted to extend it's magnitude to the face of a cube, how would I do that without physics raycasting?

To try to explain this better, how would I take Vector3(1,1,1).normalized and convert it back to Vector3(1,1,1)?

Comment
Add comment · Show 2
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 Bunny83 · Mar 24, 2013 at 01:37 AM 0
Share

Just a few things:

  • You title reads like you want to "convert" an "apple" into a "screwdriver".

  • A normalized vector or unit vector is only called normal when it's related to a surface of another object. See Surface normal. So "normal" isn't a substitute for a "normalized vector"

  • You have choosen the worst example possible. It doesn't expain what you actually wanted to do.

  • Finally downvoting is ment for clearly wrong things or misinformation. Of course you can use your karma to vote whatever you want, just want to say that i see more mis(sing)information in your question than in whebert answer. He directly answered your example.

avatar image Rush3fan · Mar 24, 2013 at 03:20 AM 1
Share

Gotcha.. yeah.. I shouldn't have downvoted him with such a poorly written question.

Webert: I upvoted you just for the effort. Looks like you were just about to come up with the same solution. Thanks!

2 Replies

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

Answer by Bunny83 · Mar 24, 2013 at 01:21 AM

It works like this:

  • figure out which component (x,y or z) has the greatest absolute value.

  • determine the factor which extends this component to 1 or -1 depending on the orientation. This is done by simply do f = 1.0f / x

  • multiply the whole vector by this factor.

So something like that:

 // C#
 Vector3 Cubify(Vector3 v)
 {
     float f = Mathf.Max(Mathf.Abs(v.x), Mathf.Abs(v.y), Mathf.Abs(v.z));
     if (f == 0.0f)
         return Vector3.zero;
     return v / f;
 }
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 Rush3fan · Mar 24, 2013 at 01:55 AM 0
Share

That works perfect! It's funny you can google "cubify vector" and nothing shows up. I don't know what the technical term is really, but as long as it works, I'm happy. :D

avatar image
1

Answer by whebert · Mar 24, 2013 at 12:27 AM

Just multiply the normalized vector by the magnitude you want it to have.

 Vector3 vector = Vector3.one;
 float magnitude = vector.magnitude;
         
 // Vector will be what it originally was
 vector = vector.normalized * magnitude;
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 Rush3fan · Mar 24, 2013 at 12:41 AM 0
Share

But you see? This question is more complicated than how to change the magnitude. I'm trying to convert a radius magnitude to a magnitude that fits inside a cube. $$anonymous$$ake more sense?

avatar image whebert · Mar 24, 2013 at 01:53 AM 2
Share

Ah, I didn't get that from your question initially. I coded the following up in Unity to see if this would work (it does), then noticed @Bunny83 had already answered... anyway.

 Vector3 vector = Random.insideUnitSphere;
         
 float cubeSize = 5f;        
 float magnitude = cubeSize / $$anonymous$$athf.$$anonymous$$ax($$anonymous$$athf.Abs(vector.x), $$anonymous$$athf.Abs(vector.y), $$anonymous$$athf.Abs(vector.z));
         
 // The vector will now lie on the cube of cubeSize
 vector = vector * magnitude;

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

12 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

Related Questions

change the length of the vector 1 Answer

Get normal direction to use as sign 1 Answer

Getting perpendicular direction vector from surface normal 2 Answers

Custom gravity based on normal vector and rigidbody.addForce 2 Answers

How do I scale a vector from another vector? 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