- Home /
Question by
gregroberts · Dec 10, 2014 at 03:27 PM ·
vector3transform.position
How can I elegantly use the XYZ of a Vector4 to set transform.position of a gameObject?
I use Vector4s data structures to store all my positional data for my gameObjects, using the W component as a metadata placeholder. Can I easily assign transform.position using the XYZ of a Vec4 while discarding the W?
Here is how I'd like the code to read:
var g_MC : new Vector4[n];
var g_MCgo : new GameObject[n];
g_MCgo[h].transform.position = g_MC[h].Vector3 + Vector3.up;
Here is the only way I can currently get the code to work:
g_MCgo[h].transform.position = Vector3(g_MC[h].x, g_MC[h].y, g_MC[h].z) + Vector3.up;
Any and all elegant help is appreciated.
Comment
Best Answer
Answer by lolzrofl · Dec 11, 2014 at 12:33 AM
You could use an extension method like this:
public static Vector3 ToVector3(this Vector4 parent)
{
return new Vector3(parent.x, parent.y, parent.z);
}
Then just do:
g_MCgo[h].transform.position = g_MC[h].ToVector3() + Vector3.up;
Your answer
![](https://koobas.hobune.stream/wayback/20220613173718im_/https://answers.unity.com/themes/thub/images/avi.jpg)