Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 Sequence · Jun 11, 2013 at 01:24 PM · vector3vector2struct

Access 2 of the 3 components of Vector3 (Vector3.xy?)

Hey guys,

I need to do something like the following

 function Main()
 {
     Vector3 vector = arrayOfVerts[0];
 
     calculationFunc(vector.xy);
 }
 function calculationFunc(Vector2 theVector2)
 {
     //Use only those components to calculate something
 }

The problem is at the vector.xy. Unlike in GLSL, you can't access components like that (apparently). What would be the most performance-wise way of doing this?

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

2 Replies

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

Answer by Bunny83 · Jun 11, 2013 at 03:09 PM

I just want to sum up some ways:

 // our vector3
 var vec = new Vector3(1,2,3);
 
 // #1 The usual way
 var tmp = new Vector2(vec.x, vec.z);
 
 // #2 Your way
 var tmp = getAxis(vec, Axis.x, Axis.z);
 
 // #3 My way ;)
 var tmp = vec.xz();

My way requires you to add this extension class to your Standard Assets folder:

 // Vector3Extensions.cs
 // C#
 using UnityEngine;
 
 public static class Vector3Extensions
 {
     public static Vector2 xy(this Vector3 aVector)
     {
         return new Vector2(aVector.x,aVector.y);
     }
     public static Vector2 xz(this Vector3 aVector)
     {
         return new Vector2(aVector.x,aVector.z);
     }
     public static Vector2 yz(this Vector3 aVector)
     {
         return new Vector2(aVector.y,aVector.z);
     }
     public static Vector2 yx(this Vector3 aVector)
     {
         return new Vector2(aVector.y,aVector.x);
     }
     public static Vector2 zx(this Vector3 aVector)
     {
         return new Vector2(aVector.z,aVector.x);
     }
     public static Vector2 zy(this Vector3 aVector)
     {
         return new Vector2(aVector.z,aVector.y);
     }
 }

It will also work from UnityScript, but Monodevelop doesn't seem to provide intellisens for the extension methods, however it works ;)

edit

Just to have it complete:

  • 1 is the fastest and most efficient way

  • 2 is the most inefficient way of the 3 because it uses the indexer of vector3 two times and involves a function call

  • 3 in the middle since it also requires an additional function call but it has the shortest syntax.

Comment
Add comment · Show 4 · 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 Sequence · Jun 11, 2013 at 07:28 PM 0
Share

Thank you for your great post! In my actual code I'm using the indexer as a way of making a function dynamic. So ins$$anonymous$$d of having calculateUsingXY and calculateUsingZY I pass two integers that serve as the lookup indexes. The get axis was just an example (sorry about that). Are lookup indexes less efficient than Vector3.x?

avatar image Bunny83 · Jun 12, 2013 at 12:30 AM 0
Share

Well, indexers are just special properties, so reading them is also a function call. Besides that the indexer is implemented like this:

 public float this[int index]
 {
     get
     {
         switch (index)
         {
         case 0:
             return this.x;
         case 1:
             return this.y;
         case 2:
             return this.z;
         default:
             throw new IndexOutOfRangeException("Invalid Vector3 index!");
         }
     }
     set
     {
         switch (index)
         {
         case 0:
             this.x = value;
             break;
         case 1:
             this.y = value;
             break;
         case 2:
             this.z = value;
             break;
         default:
             throw new IndexOutOfRangeException("Invalid Vector3 index!");
         }
     }
 }

That means each look up will cause 1 to 3 comparisons

avatar image Sequence · Jun 15, 2013 at 04:01 PM 0
Share

Excellent in-depth knowledge! Thanks!

avatar image Fattie · Jan 21, 2015 at 05:25 AM 0
Share

FTR bud, you forgot the UnityEngine. on the declaration line of the functions, or, the "using" ! :)

avatar image
1

Answer by Sequence · Jun 11, 2013 at 02:40 PM

EDIT: See accepted answer and explanations that go with it, much better than mine.

Future reference for anyone coming across this in the future. This is what I did. Vector3s' components can be accessed like an array.

 Vector3 bobTheVector = new Vector3(1, 2, 3);
 
 float oneAxis = bobTheVector[0];

Here's the way I implemented it.

 enum Axis : int {x = 0, y = 1, z = 2}
 
 function getAxis(Vector3 theVector, int a, int b)
 {
      return Vector2(theVector[a], theVector[b]);
 }


The enum is just incase you want to do something like Axis.x instead of remembering that 0 is the x axis.

 getAxis(new Vector3(1, 2, 3), Axis.x, Axis.z);
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

16 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

Related Questions

Closest point on multiple lines? 2 Answers

C# Point structure 1 Answer

2D Vectors along 3D Triangle 0 Answers

How to use 2D pathfinding with vector 2 0 Answers

conserve momentum of vector3.moveTowards in 2D game 0 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