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 Aubrey Hesselgren · Aug 16, 2010 at 11:31 AM · vector3vector2normalize

Vector2 Normalize ?

There doesn't seem to be a normalize function for Vector2's.

If I use a 2D vector inside the Vector3.Normalize static function, will that deal with it happily? Similarly, if I set a 2D vector to a 3D one, how will it handle it? Just pass back the first two components (X and Y)?

i.e. is this valid:

Vector2 delta = vTo - vFrom;
//... ignore delta mangitude == 0.0f
Vector2 direction = Vector3.Normalize( delta );

Thanks in advance!

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

3 Replies

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

Answer by Herman-Tulleken · Aug 16, 2010 at 12:28 PM

It seems like it does work. I can't see by which mechanism this is possible (perhaps an auto-conversion). You could also use the following (it is probably faster):

Vector2 direction = delta.normalized;
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
avatar image
1
Best Answer

Answer by Peter G · Aug 16, 2010 at 01:14 PM

Did you try casting? You cannot implicitly cast from Vector3 to Vector2 because you will lose the z value.

Vector2 direction = (Vector2)Vector3.Normalize(delta);
Comment
Add comment · Show 6 · 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 Aubrey Hesselgren · Aug 16, 2010 at 02:14 PM 0
Share

I actually want to lose the Z! The delta didn't have any in the first place in this example, so for all intents and purposes, it's just junk data. Obviously, that's inefficient, but it suits my purposes :). Thanks for this!

I guess casting is a better practice for this, just to keep the conversion explicit.

avatar image Aubrey Hesselgren · Aug 16, 2010 at 02:16 PM 0
Share

Oh, and I guess I probably want to cast the delta to a Vector3, also.

avatar image Peter G · Aug 16, 2010 at 02:29 PM 0
Share

It's fine that you want to lose the z, but the compiler doesn't know that. It assumes that you are making a mistake and are going to accidentally throw away your z value so you even if you want to lose it, you have to tell it that.

avatar image Peter G · Aug 16, 2010 at 02:30 PM 0
Share

Edit: you don't need to cast delta to a Vector3 because that's implicit. The z value is assumed to be 0.

avatar image Aubrey Hesselgren · Aug 16, 2010 at 05:19 PM 0
Share

Thanks Peter! I'll try to remember the implicit cast cases.

Show more comments
avatar image
2

Answer by Wolfram · Aug 16, 2010 at 04:22 PM

You can help yourself with the .magnitude field, which is available for Vector2, so there will be (probably) no internal casting.

Vector2 delta = vTo - vFrom;
Vector2 direction = delta/delta.magnitude; // maybe test this for ==0 first

EDIT: To complete the answer: Vector3.Normalize(v) modifies and normalizes vector v. It does not return anything, so you can't use it at all here. Either use this method I described here, or use delta.normalized, as Herman Tulleken suggests.

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

1 Person is following this question.

avatar image

Related Questions

Normalizing a vector 1 Answer

Writing Policy class for handling Vector2s and 3s. 1 Answer

When does Unity calculate vectors' magnitudes? 1 Answer

How to get vector X distance from point on Y angle? 4 Answers

How to cast Vector3 on a plane to get Vector2? 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