- Home /
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!
Answer by robertbu · Nov 13, 2014 at 06:58 AM
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);
Definitely hits the nail on the head. Thank you! Another thing I've learned from this response is I asked the wrong question, I asked for a function when I should have asked for code itself, I think it would of given me more info.
Your answer
Follow this Question
Related Questions
math and statistics functions 4 Answers
All Mathf functions 1 Answer
Movement behaves differently on X axis 0 Answers
Pass function into a shader 0 Answers