The player clips trough the ground when gliding?

I am creating a 3D platformer where the main character is a bird, I am creating a script to make the player glide with its wings.

I have a problem with my script, The player clips trough the ground when holding the glide button, I cannot really find the problem but I know it must be something with raycasting, but raycasting is a nightmare to me

Anyways, here is my script

local player = game.Players.LocalPlayer

local RunService = game:GetService("RunService")
local ContextActionService = game:GetService("ContextActionService")

local char = script.Parent

falling = false

char.Humanoid.StateChanged:Connect(function(_,state)
	if state == Enum.HumanoidStateType.Freefall then
		falling = true
		wait()
	elseif state ~= Enum.HumanoidStateType.Freefall then
		falling = false
		wait()
	end
end)

local function Glide(actionName, inputState)
	if inputState == Enum.UserInputState.Begin and falling then
		print("Glide active!")
		script.Parent.Falldamage.TakeDamage.Value = false
		local lowgrav = Instance.new("BodyVelocity")
		lowgrav.Parent = char.Head
		lowgrav.MaxForce = Vector3.new(0, 100000, 0)
		lowgrav.P = 10000
		lowgrav.Velocity = Vector3.new(0, -10, 0)
		lowgrav.Name = "LowGravity"

	elseif falling == false or inputState == Enum.UserInputState.End or inputState == Enum.UserInputState.Cancel or inputState == Enum.UserInputState.None  then
		print("Glide inactive!")
		local lowgrab = char.Head:FindFirstChildOfClass("BodyVelocity")
		script.Parent.Falldamage.TakeDamage.Value = true
		if lowgrab then
			char.Head.LowGravity:Remove()
		end


	end
end

game.ContextActionService:BindAction("Q", Glide, true, Enum.KeyCode.Q, Enum.KeyCode.ButtonA)

I am sorry for any spelling mistakes, I am not american and just bilengual

(P.S. I am sorry if there is a part of the script that is mildly infuriating, I am not that good of a scripter.)

Quick fix

Change

		lowgrav.MaxForce = Vector3.new(0, 100000, 0)

to

		lowgrav.MaxForce = Vector3.new(0, 5000, 0)

That way, you aren’t trying to force the character into the ground so hard.

Other options

  1. Remove the BodyVelocity (or set its MaxForce to 0,0,0) whenever the character is touching the floor.

  2. Attach to RunService.Stepped and manually limit the max downward velocity.

  3. Instead of applying a BodyVelocity downwards, apply a BodyForce upwards, with the Force equal to some fraction of gravity like:

    -- recursively get the mass of a model, part, etc.
    local function GetTotalMass(root)
    	local mass = 0
    	if (root:IsA("BasePart")) then
    		mass = root:GetMass()
    	end
    	for _, child in pairs(root:GetChildren()) do
    		mass += GetTotalMass(child) 
    	end
    	return mass;
    end
    
    -- ...
    
    local lowgrav = Instance.new("BodyForce")
    lowgrav.Force = Vector3.new(0, GetTotalMass(char) * workspace.Gravity * 0.75, 0)
    lowgrav.Parent = char.HumanoidRootPart
    

    (although this would be more of a moon-gravity situation than a constant downward velocity)

1 Like

For some reason, I changed the velocity of the BodyForce and now the fall damage is always disabled?

Do you mean the MaxForce of the BodyVelocity?

1 Like

Yeah, I meant to say BodyVelocity lol