Hover Vehicle Moving Unusually : /

Hey there!

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

https://gyazo.com/3c1e7638e146f9c9f3ac2957650d0361

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?

1 Like

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.

1 Like

Hey I didn’t understand the last line. why are we changing the hrp of the player?

also you’re only taking playerMass in consideration for hoverforce, so in my case, Should i say

hoverforce = (PlayerMass + HovercraftMass) * gravity ?

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.

Written on mobile

1 Like

Yeah I thought of that, but then BodyPosition didn’t give the smooth movement that’s why I moved to bodyforce

Hmm that’s weird. I’d recommend using a BodyForce (to counteract gravity) and a BodyPosition. In what way was a BodyPosition not being smooth?

Sometimes it can over correct or the force moves it to the position too fast.

1 Like

yea it corrects it too fast. Ill try playing with the Dampening tho. Thanks!

1 Like

also the upward force should be (PlrMass + BikeMass) * grav right?

1 Like

Yep. You might not need to set it back to just the bike mass because it probably won’t move much without a player.

1 Like

aight ill play around with it thanks

1 Like

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.

1 Like