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 Benjamin · Oct 11, 2010 at 08:56 AM · vector3vector2convert

How do I use the Vector2/Vector3 operator (converting Vector2 to Vector3 and vice versa)

The Unity reference describes the following for converting a Vector2 to Vector3.

*Vector2.operator Vector3
static implicit function Vector3 (v : Vector2) : Vector3 Description Converts a Vector2 to a Vector3.

Vector2s can be implicitly converted to Vector3 (z is set to zero in the result).*

I've played around with this a bit, but I'm not sure how to get it working - could someone show me an example.

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
10
Best Answer

Answer by Jessy · Oct 11, 2010 at 09:30 AM

It means you can do stuff like this:

var o1o : Vector3 = Vector2(0,1);   // UnityScript
Vector3 o1o = new Vector2(0,1); // C#

(However, you can also get the same result from using the "xy" Vector3 constructor):

var o1o : Vector3 = Vector3(0,1);   // UnityScript
Vector3 o1o = new Vector3(0,1); // C#

This kind of thing starts to become helpful when you have functions that take a Vector3 as a parameter, but you're only working in 2D. For example, maybe you want to create a UV transformation matrix with Matrix4x4.TRS. In that case, the position variable is only going to be a Vector2, as UV space is two-dimensional. So, despite the fact that the function requires a Vector3, you can send it a Vector2 without any problem.

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 Benjamin · Oct 12, 2010 at 02:45 AM 0
Share

Thanks Jessy. It was the exact syntax I wasn't sure of - I couldn't deduce it from the reference text.

avatar image yoyo · Sep 06, 2011 at 10:19 PM 2
Share

"Helpful" unless your 2D world is in the x-z plane, and you want the conversion from Vector3 to Vector2 to extract x and z. Of course you can still do the conversion manually, but coding errors can get silently hidden by Unity's automatic conversion.

avatar image
0

Answer by roberto_sc · Apr 19, 2015 at 10:17 PM

That means they have overloaded the attribution operator "="; therefore you can simply attribute a Vector3 to Vector2 and vice-versa.

 Vector2 v2 = Vector3.one;
 Vector3 v3 = Vector2.one;

You don't even need to cast.

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
0

Answer by luislodosm · Mar 20, 2017 at 02:41 PM

 public Vector2 myVector2D;
 public Vector3 myVector3D;
 
 Vector2 myResult2D = myVector2D + (Vector2) myVector3D; 
 Vector3 myResult3D = (Vector3) myVector2D +  myVector3D; 
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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Convert Vector2 to Vector3 based on the ground's height? 1 Answer

C# Convert Vector3[] to Vector2[] 3 Answers

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

Inconsistent wallJumping, vector sends in wrong direction 0 Answers

Normalizing a 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