- Home /
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?
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.
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?
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
FTR bud, you forgot the UnityEngine. on the declaration line of the functions, or, the "using" ! :)
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);
Your answer
Follow this Question
Related Questions
2D Vectors along 3D Triangle 0 Answers
How to use 2D pathfinding with vector 2 0 Answers
Closest point on multiple lines? 2 Answers
C# Point structure 1 Answer
How can I translate a Vector2 control system to Vector3 and rotate? 1 Answer