Problems with Vector3.Unit and using it for raycasts

I’m trying to make a fall/ velocity damage based system, which works by getting the normalized direction of the characters velocity, and ray casting in said direction. This allows me to detect fall damage from every direction with one raycast instead of having to make multiple raycasts.

So step 1, I create my raycast

local rayCastOffset = Vector3.new(0, -10 ,0)
...
local raycast = workspace:Raycast(character.HumanoidRootPart.CFrame.Position, (character.HumanoidRootPart.Velocity.Unit * 2) + rayCastOffset, params)

rayCastOffset is used to start the raycast at the feet of the player since this raycast is also used to determine if the player is grounded.

I attached a debug part to the destination of this raycast:

debugPart.Position = (character.HumanoidRootPart.Velocity.Unit * 2) + rayCastOffset + character.HumanoidRootPart.Position

But I get this result:

The block starts a bit too far down but that can be adjusted by tweaking rayCastOffset. My main issue is the fact that the block doesn’t move upwards. After printing the Vector3.Unit of my character’s velocity I get this vector: (0, 1, 0)

Of course, a normalized vector will always have a magnitude of 1, and that makes it mathematically impossible to have a vector of (0, 0, 0) but I find it extremely counter-intuitive that I can’t tell if a part is at rest or if it’s moving upwards. I made this post to ask if there was a better way to get a normalized vector or would I have to do my own math and normalize the vector myself. Does Roblox have a better way?

4 Likes

The parameters of Raycast function is Origin, Direction, otherParams, you’re adding your raycast offset to the direction not origin. I believe you want your raycast to be cast from your feet.
Your normalized value is probably 1 because physically controlled objects still do have a velocity even in standing. These values are minimal, but not 0. That happens to every engine known, it’s normal behavior. You have to add your own check if the length of the velocity is bigger than some tolerance value defined by you. You can calculate the length with vec3val.Magnitude, if you only care about Y velocity, check it on Y value.

I don’t want the raycast from the foot since the character does get ragdolled and collisions may occur from other areas. I was hoping roblox had a built in method for normalizing vectors that didn’t involve (0, 1, 0)

I’ve just gone and used a simpler solution for my code:

		local velocityVector
		if character.HumanoidRootPart.Velocity.Magnitude < 1 then
			velocityVector = character.HumanoidRootPart.Velocity
		else
			velocityVector = character.HumanoidRootPart.Velocity.Unit
		end
1 Like

Yes, that’s what I’ve already told you.

1 Like

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.