Trying to cap boat speed

right now, i added thrust to my boat; i’m just unaware as to how i can cap the speed of my boat.
the max speed i want is from the vehicleseat’s maxSpeed property. ty!

function boat:HandleThrust()
	local rs = game:GetService("RunService")
	
	if not self.connection then
		self.connection = rs.Heartbeat:Connect(function(dt)
			if self.vehicleSeat.ThrottleFloat ~= 0 then
				if not self.startTime then
					self.startTime = tick()
				end
				self.currentTime = tick() - self.startTime
				self.thrust.Force = Vector3.new(self.vehicleSeat.ThrottleFloat * self.currentTime * self.TotalMass, 0, 0) * self.vehicleSeat.CFrame.LookVector
				self.velocityMagnitude = self.thrust.Force.Magnitude / self.TotalMass
			else
				self.startTime = nil
				self.currentTime = nil
				self.thrust.Force = Vector3.zero
			end
			
			print("thrust: ", self.thrust.Force)

		end)
	end
end
2 Likes

Look into math.clamp

2 Likes

I second this, additionally this thread is a bit old, but might have some nuggets you’ll find useful.

1 Like

the thrust of the boat is the length of the thrust vector thrust.Force.Magnitude
if its greater than the maxSpeed property than you can normalize it and then multiply with thrust.Force = thrust.Force.Unit * maxSpeed

1 Like

tysm!!! i actually did stumble across that post a few weeks ago. i have to understand it a bit tho haha

ive used math.clamp before but i wouldnt really know how to implement it in my situation tbh

hey man,
tysm for ur help! i took your suggestion and did something myself. the thing is is that when i printed out the
thrust.force.Magnitude, it was printing quite a large number as the boat accelerated more and more like so:

image

i did something of my own and it does cap the speed however, the vector force flickers. i assume its because of the else statement overriding the force and stuff. its hard to explain. i made a video to show the force of the thrust when it hits max speed.

			if math.abs(self.vehicleSeat.AssemblyLinearVelocity.X) >= self.vehicleSeat.MaxSpeed then
					self.thrust.Force = self.thrust.Force.Unit
				else
					self.thrust.Force = Vector3.new(self.vehicleSeat.ThrottleFloat * self.currentTime * self.TotalMass, 0, 0) * self.vehicleSeat.CFrame.LookVector
				end

i think this is easier if you just clamp the time

--sets to the smallest number between itself and maxspeed, so if it goes over the maxspeed then it is equal to the max speed
--also have to adjust maxspeed since its in terms of time now
self.currentTime = math.min(self.currentTime, self.vehicleSeat.MaxSpeed) 
self.thrust.Force = Vector3.new(self.vehicleSeat.ThrottleFloat * self.currentTime * self.TotalMass, 0, 0) * self.vehicleSeat.CFrame.LookVector
1 Like

i’ve spent around 2-3 days researching on how to cap the speed with vectorforce but it’s a bit too advanced for me at the moment.

i used linearvelocity instead and it works now thanks to this video and to happy for contributing so i could clamp the time for more realistic acceleration!

function boat:HandleThrust()
	local rs = game:GetService("RunService")

	if not self.connection then
		self.connection = rs.Heartbeat:Connect(function(dt)
			if self.vehicleSeat.ThrottleFloat ~= 0 then
				if not self.startTime then
					self.startTime = tick()
				end
				
				self.currentTime = math.min(tick() - self.startTime, self.vehicleSeat.MaxSpeed)
				self.thrust.LineVelocity = self.vehicleSeat.ThrottleFloat * self.currentTime
				print("CurrentTime: "..self.currentTime)

			else
				self.startTime = nil
				self.currentTime = nil
				self.thrust.LineVelocity = 0
			end
		end)
	end
end

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.