Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 W.Walter · Mar 27, 2016 at 10:51 AM · scripting problem

How are Vector2 and Vector3 interchangeable from a code standpoint

Title says it all, from a code perspective, how were Vector2 and Vector3 made interchangeable. Is it an implicit cast? An inheritance thing?
Simple code example of how they achieved it would be appreciated.

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 TobiasW · Mar 27, 2016 at 11:12 AM

Implicit and explicit casting is made possible by defining conversion operators. Here is a pretty good description with an example for both: https://msdn.microsoft.com/en-us/library/85w54y0a.aspx

Comment
Add comment · Show 3 · 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 troien · Mar 27, 2016 at 12:25 PM 1
Share

To add to this answer, link to the docs of these operators.

Vector2 to Vector3

Vector3 to Vector2

ps. the Vector3 to Vector2 is at a strange location in the docs, this is probably because the docs are generated, and sinse operators are static they don't have to be placed at a location that makes sense. So Unity placed the operator in the Vector2 class ins$$anonymous$$d of the Vector3 class :p

As for the code used, probably something like this:

 public static implicit operator Vector2(Vector3 other)
 {
     return new Vector2(other.x, other.y);
 }
 
 public static implicit operator Vector3(Vector2 other)
 {
     return new Vector3(other.x, other.y, 0f);
 }

Aso on a small sidenote, when implementing implicit operators yourself, the Vector3 to Vector2 might not be a good example, because it loses data. Whenever you lose data in the conversation you generally want to use an explicit operator. (see implicit docs)

avatar image meat5000 ♦ · Mar 27, 2016 at 01:33 PM 1
Share

Just to add that casting is all good but to be perfectly frank here, when the need arises to convert from one to the other it usually follows that you require a manual conversion.

The linked $$anonymous$$ethods both utilise x and y and z is discarded or zeroed.

$$anonymous$$ost of the time I have required to convert a Vector3 to a Vector2 it is usually that I require the x and z axes and want to ignore the y, so I have no choice other than a manual conversion.

 Vector2 myVec2 = new Vector2(myVec3.x, myVec3.z);

This happens a lot when using ScreenPoint to find the coords on a terrain, for example, when the y position is largely unused.

avatar image W.Walter · Mar 27, 2016 at 09:04 PM 0
Share

Its a productive day when I learn something new about C# Thanks all for your thoughts, much appreciated. :D

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

44 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

Related Questions

How do I manage collisions between characters being moved around by their transform.position? 2 Answers

SphereCast doesn't work when I increase the radius. 1 Answer

OnPreRender for Handles.DrawCamera 0 Answers

Unity how to access animation states (from blender) with C# ONLY 0 Answers

script does not work when you open the Unity 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