What is vector3.magnitude?

I know what a vector3 is, but what is .magnitude and how would I use it, or why? Can someone please explain .magnitude so I can better understand it’s uses.

The magnitude is the distance between the vector’s origin (0,0,0) and its endpoint. If you think of the vector as a line, the magnitude is equal to its length.

10 Likes

With two vectors, a and b, then (a-b).magnitude is the distance between them. That’s what Vector3.Distance() does actually. Since rigidbody.velocity is a vector, then rigidbody.velocity.magnitude is how fast a rigidbody is going.

–Eric

10 Likes

Thanks a bunch, I was using it and yet I didn’t fully understand it.

haa~~.I know… Thanks…

magnitude PK distance

how can i set rotation my 2D sprite using button UI??

A necro non-sequitor. Nice.

8 Likes

Man, I’m actually kind of curious how he even got to this thread for that kind of question.

7 Likes

For such off topic …

3618763--294583--upload_2018-8-29_18-16-39.png

After pressing UI button, sprite will rotate in the feeder. Works for both 2D and 3D.

16 Likes

Most likely the same way he found the other thread when he made his first post…

https://forum.unity.com/threads/cant-find-the-iskinematic-on-off-pick.469802/#post-3600193

2 Likes

Hello, sorry for necroposting. As said Vector3.Magnitude is “The length of the vector is square root of (xx+yy+z*z).” and as Vector3.Distance() is “(a-b).magnitude”, so can i assume that Vector3.Distance() always returns a positive number?

I mean make a new post?

Also yes, distances are larger than 0 in euclidean spaces, like Unity or the real world.

1 Like

Sort of. You can assume that it returns a nonnegative number as long as the inputs are not garbage. If you feed two identical vectors in, you can get 0, and if you feed in a vector with a NaN component, you can get NaN.

1 Like

Magnitude of a vector is a standard math thing. Likewise distance between points. The internet is full of non-Unity explanations of how to compute it, what it means, why the formula works, why it’s always positive, and so on. It’s nice knowing that many general concepts work the same way in Unity.

A head’s-up: Khan Academy’s explanation isn’t as hilarious as usual. It has the “can I do this again without so many mistakes and better examples?” feeling, but you won’t snort milk out your nose.