I was trying Hovercrafts today and I used raycasts to check altitude.
Here’s my code.
while wait(0.6) do
local raycastParams = RaycastParams.new()
raycastParams.FilterType = Enum.RaycastFilterType.Blacklist
raycastParams.FilterDescendantsInstances = {script.Parent.Parent.Parent}
local raycastResult = workspace:Raycast(script.Parent.Position, Vector3.new(0,-1,0).Unit * 100, raycastParams)
if raycastResult then
local distance = (script.Parent.Position - raycastResult.Instance.Position).Magnitude
print(raycastResult.Instance:GetFullName())
if distance < 22 then
script.Parent.BodyForce.Force = Vector3.new(0,5550,0)
print("too close")
else
script.Parent.BodyForce.Force = Vector3.new(0,0,0)
end
print(distance)
end
end
And something happens with it, which is expected, ngl
So what happens is, when the bike reaches at the limit (22), then it’s force goes to zero, which makes it fall suddenly. Is there anything I could do to almost fix it on the limit, but at the same time make give it smooth movement?
You can get the mass of the vehicle and calculate how much force need to be applied to make the vehicle approximately float while maintaining constant height. You would have to include the number of players driving of course.
Changing the mass of your vehicle could help too. There is a fully working code pasted in the following post, regarding player hovering. Originally, the script belongs in StarterCharacterScripts, but you would of course have to adjust it for your hovercraft. You should be fine with it, but having multiple players on board of, for example hovering bus, might be a different slightly different story. I would have to test that to confirm.
I’d recommend using a BodyPosition instead of a BodyForce. If you use a body position, you can make the bike hover in place. If the ray is too short, you can increase the Y component of the .Position property. If it’s too long, you can increase the Y component. To make it so the BodyPosition only moves up and down you can set the .MaxForce property to something like: Vector3.new(0, Force, 0).
What is happening in your video is that the bike can only be moving upwards or downwards so it goes up and down continuously.
Factoring in gravity and using a BodyForce would also work though not nearly as well as a BodyPosition.
hey is there a way i could use BodyThrust or force? Cuz i want leaning too, suppose the front wheel is on a slope, ill want to turn the whole thing upwards, and bodyposition cant help there
You could use a BodyGyro then have two rays, one in front and one if back. For the BodyPosition you could calculate the mid point between the two and then for the BodyGyro you can make a new CFrame with lookAt using the midpoint Vector3 and the front Vector3.
You could use body forces for this though you might need some physics formulas and your loops would need to be connected into something faster like RunService.Heartbeat.