Hovering part help

I’m having issues with my part hovering above the ground

local Part = script.Parent
local Body = script.Parent.BodyPosition
local CarBody = workspace.Part

local raycastParams = RaycastParams.new()
raycastParams.FilterDescendantsInstances = {}
raycastParams.FilterType = Enum.RaycastFilterType.Whitelist
raycastParams.IgnoreWater = false
local RunService = game:GetService("RunService")

function Run()
	local R = Ray.new(Part.Position, Vector3.new(0,-4,0)) 
	local PartFound, Pos = workspace:FindPartOnRay(R) 
	local DistanceToGround = Part.Position.Y-Pos.Y
	local Var = CarBody.Size.X * CarBody.Size.Z * 500
	Body.Position = Vector3.new(Part.Position.X, DistanceToGround, Part.Position.Z)
	Body.MaxForce = Vector3.new(Var, Var, Var)
	print(DistanceToGround) 			
	--print(Pos.Y) 
	--print(Body.MaxForce)
	--print(Body.Position)
end

RunService.Stepped:Connect(Run)

What happens is this :

What I’m trying to do is make the part hover a X amount of studs above the ground all the time
Like this :

I’ve tried many things all day long and couldn’t find my answer.
Thanks for any help in advance! :slight_smile:

Try casting it more than 4 studs down (currently it is set to -4 for the up vector). It shouldn’t affect the performance too much. I’d recommend around 30 studs to be safe.

There’s code in this post that visualises ray casts.

Also, consider switching to WorldRoot:Raycast.

2 Likes

I tried a lot of different things with your suggestion and it wasn’t working for me, thanks for the help though.

1 Like