# Function for returning x-length from vector made from given length and direction.

New to Unity, I used to use Game-Maker a lot. A function I need to use in Unity was in Game-Maker, and I’m looking for it:

``````lengthdir_x(len,dir) --Returns the horizontal x-component of the vector determined by the indicated length and direction.
``````

So in a nutshell I need a method that when given a length and direction can calculate the total horizontal distance from the start point to the end point of the vector. I know this could easily be solved with some basic math, but I do not know the functions in Unity well and I can’t seem to find good documentation. If anyone could recommend a good source, I’d also be grateful for that. Cheers!

I think this is what you are asking for:

``````function lengthdir_x(len : float, dir : Vector3) : float {
dir = dir.normalized * len;
return dir.x;
}
``````

Note this returns the x component. If you want the magnitude of the x component, then change the return to:

`````` return Mathf.Abs(dir.x);
``````