- Home /
Changing vector direction without computing magnitude
I would like to update the direction of gravity based on the normal of a water plane whose rotation can change (yeah it's crazy I know). Currently I am doing :
Physics.gravity = water.transform.up.normalized * -Physics.gravity.magnitude;
And it works as expected. However, computing the magnitude has a performance cost, so I would like to avoid it. What could I do ? Thanks.,
Answer by villevli · Nov 22, 2017 at 10:52 PM
If you're not changing the Physics.gravity elsewhere you can just store the magnitude you want into a float variable and use that. You also don't have to normalize the Transform.up/forward/right vectors.
Physics.gravity = water.transform.up * myGravityMagnitude;
I've converted your comment into an answer - this way others can find your suggestion more easily. By the way, there's no need to normalize the transform.up vector: it's already normalized.
Thanks for the help. I was more thinking about a built-in way to change the direction, like a satic Vector3 method or something, rather than simply caching. But that could do the trick too.