I’m not sure if this is intended behavior. If it is, it doesn’t seem like it should be.
If you read the unit value of a Vector3 whose axis values are all 0, it gives you a NAN Vector3.
> print(Vector3.new().unit)
NAN, NAN, NAN
I was trying to write a function that returns the Velocity of the character’s HumanoidRootPart along the XZ axis, with a maximum speed of 16. When the character stood still, it would return NAN values for everything.
local function getPlayerMotion()
local velocity = root.Velocity * Vector3.new(1,0,1)
if velocity == blankV3 then -- Causes a NAN error.
return blankV3
else
local unit = velocity.unit
local speed = min(16,velocity.magnitude)
return unit * speed
end
end
From a logical standpoint, I can understand why it returns what it does, but I don’t think anything useful can come of using a NAN Vector3. It causes parts to disappear entirely, and I think it would make sense if there was a safe-guard in place for this specific instance.
If this isn’t a good enough reason, so be it, but it seems silly that we have to implement safe guards for behavior that isn’t desirable or usable.
What else could it be, though? A unit vector has to have magnitude 1, so returning <0, 0, 0> would be incorrect, and if it did return that, then whenever you accessed .unit you’d need to make sure it has a magnitude of 1, which would also be a silly safeguard.
The underlying issue here is that the zero vector has no direction, so “the same direction as the original but a magnitude of 1” doesn’t make any sense.
Ah, that makes sense.
Well, as annoying as it is, I guess I can’t fight logic.
I just wish Roblox would provide us with built-in API to combat NAN errors.