- Home /
"normalize" a vector but conserve the "- sign"
Hi there !
I understand how normalize work but I'd like to know if there is a way to normalize the "magnitude" and NOT the direction/sens
I mean if my vector2is x=-3.4 and y=4, I'm looking for a method that gives me Vector2(-1,1)
Thanks for you help ! :)
I am confused at what you are trying to do.
If you give the Vector2(-3.4,4.0) and normalize it the new vector2 should be Vector2(-0.64764842,0.761939318) not Vector2(-1,1)
Normalize keeps the magnitude of the new Vector2 to 1, but preserves the direction.
What you describe here is not "normalizing". A normalized vector has an overall length / magnitude of 1.0. The vector (-1, 1) is not normalized. It has a length of "sqrt(2)" (about 1.41421)
Your original vector (-3.4, 4) has a length of about 5.248. When you actually normalize the vector the result would be approximately (-0.6476, 0.76194). This vector is normalized (has a length of 1.0 and does still preserve the direction)
I think you should be more clear about what you want to do.
Just for reference, this is how normalizing works:
public static Vector2 Normalize(Vector3 aVec)
{
float len = $$anonymous$$athf.Sqrt(aVec.x*aVec.x + aVec.y*aVec.y);
return new Vector2(aVec.x / len, aVec.y / len);
}
So when you have a given vector "v" those lines do exactly the same:
v = Normalize(v);
v = v.normalized;
Answer by leech54 · May 18, 2017 at 11:04 AM
I am confused at what you are trying to do.
If you give the Vector2(-3.4,4.0) and normalize it the new vector2 should be Vector2(-0.64764842,0.761939318) not Vector2(-1,1)
Normalize keeps the magnitude of the new Vector2 to 1, but preserves the direction.
Answer by NoBrainer-David · May 15, 2017 at 06:13 PM
You probably want to use the Mathf.Sign function. Like so;
Vector2 myVector = Vector2(-2.0f, 1.3f); // some value
myVector.x = Mathf.Sign(myVector.x);
myVector.y = Mathf.Sign(myVector.y);