What do you mean by “the normal of a vector”? Do you mean a normalized vector? If you normalize a vector, you can get the original vector by multiplying the normalized vector by the magnitude. Is that what you mean?
Edit: Oh, did you mean the "norm" of the vector? I haven’t been to school in a long time.
Yes it’s relevant in Unity. There is no magic that makes the “magnitude” and “normalized” properties of a vector affect each other if you call one of them first.
If you have the magnitude you can safely divide the vector by its magnitude and get the normal vector (or “unit sphere” version of the vector).
If you have the normal of the Vector, you can safely calculate the magnitude by dividing any of its components by the normal’s corresponding component. (assuming at least one of them is nonzero…)
All of this is subject to the usual floating point math precision limitations.
Normalizing a vector first computes the magnitude, then divides by it. That means if you already have the magnitude, or more commonly, you need m and n, n=v/m is a slightly faster way to compute the normal. You may as well use it (your 2nd snippet).
A rule in programming is to never trade readability for a speed-up you probably won’t even need. But most people know the n=v/m trick, and if you name it vNormalized it’s even more obvious. Even if you later find you don’t need the magnitude, you’re still no worse on speed than calling normalize. The one case I don’t bother with is in experimental code. For that I’ll sometimes write out v3.normalized every place I need it, since it wastes less of my time. If that code somehow stays unchanged (which never happens) I can optimize it later.
Sorry no, I meant a normalized vector. In this case a unit vector from the original. Or original.normalize. My ability to use words to question math is lacking. I typically don’t ask the right way, use the right terms, mostly because I don’t know any better, which isn’t much of an excuse
So, In my case I usually always got the normalized vector first and then used the magnitude = dot(v, v.normalized). Do you know if doing it this way is slower that doing n = v/m? I hated dividing by anything for fear of division by 0 in code. An irrational fear, but its right up there with trying to debug nan and null.
You usually have to check for a zero vector anyway. For example, if you’re moving towards a spot, you’ll need to check if you’re there yet.
As far as the amount of math, add it up. Computing m then n=v/m is exactly the same work as running normalize. Normalize + m=dot(v.n) is clearly more work (dot is 3 multiplications and 2 additions). Plus it seems more difficult to read (I didn’t even know that relation).
That makes sense. I’m sure you did know about the dot relation, though. Dot(unitVector, vector) gives you the projection of vector along unit vectors direction, and a vector projected on itself is its magnitude. I still can’t wrap my head around all this math stuff though. Give me two two weeks and I’ll have forgotten all of this only to be banging my head against the monitor again.