How to get only one axis in magnitude

Don't reply for now.

I had a fall damage script, and I’m using raycasting for it (someone recommended I do it). And I encountered a bug where, if the player moved mid-air, the ray was offset from the character, and I don’t take damage. So I was wondering how to calculate it from only Y-axis.

An example of what I want to achieve is, let’s say the player jumps off a ledge at a position of 10 on the Y-axis, and lands at a position of 3 on the Y-axis. The player also moves by 1 stud on the Z-axis. Even though the player moved 1 stud on the Z-axis, it just calculates the distance on the Y-axis, and gets 7.

I have tried the “HowToRoblox” tutorial on it, which was my original code, and that was what I was having issues with.

Here’s the code.

local module = require(game.ReplicatedStorage.GlobalFunctions)

game.Players.PlayerAdded:Connect(function(p)
	p.CharacterAdded:Connect(function(c)
		local startPos
		c.Humanoid.StateChanged:Connect(function(old, new)
			if new == Enum.HumanoidStateType.Freefall then
				startPos = c.HumanoidRootPart.Position.Y
				print("'startpos' set")
				local ray = Ray.new(c.LeftFoot.Position, -c.LeftFoot.CFrame.UpVector * 5000)
				local part, pos = workspace:FindPartOnRay(ray, c)
				repeat wait() until (c.LeftFoot.Position - pos).magnitude <= 1
				local landPos = c.HumanoidRootPart.Position.Y
				if startPos < landPos then return end
				local fallDamage = module.RoundNumber((startPos - landPos) / 2)
				c.Humanoid:TakeDamage(fallDamage)
				print(fallDamage)
			end	
		end)
	end)
end)

It would be helpful if this is resolved as soon as possible, as it is 11 PM, and I want to go to bed.

Thanks in advance.

1 Like

repeat wait() until (c.LeftFoot.Position.Y - pos.Y) <= 1

1 Like