Need help with math.clamp and multiple boundaries

Hey everyone, I am currently creating a custom character object for my top down shooter game that does not have any applied physics. My goal with this was to have set boundaries around the map so the fake character cannot walk through walls, however I ran into an issue and that is being able to seamlessly pass to a different path even if it is on an angle. Now I have put a small approach into this but cannot seem to figure out how to go outside of the original boundary. Now yes, I could use a raycast to find the next boundary but then if it is a different size it will surely have a weird cframe effect, and so that led me to be stumped. If anyone can help me with this, that would be very much appreciated : )

Video Example:

Code:

local function ClampBounds(HitPos)
	local ModelSize = (Vector3.new(math.abs(MovePart.Size.X),math.abs(MovePart.Size.Y), math.abs(MovePart.Size.Z)))/2
	
	local MinX, MaxX 
	
	for _, Plot in ipairs(Plots) do
		local PlotSize = (Vector3.new(math.abs(Plot.Size.X),math.abs(Plot.Size.Y), math.abs(Plot.Size.Z)))/2
		local PlotCF, PlotPosition = Plot.CFrame, Plot.Position
		
		MinX = -(PlotSize - PlotPosition) + ModelSize
		MaxX = (PlotSize + PlotPosition) - ModelSize
 	
		local X = math.clamp(HitPos.X, MinX.X, MaxX.X)
		local Z = math.clamp(HitPos.Z, MinX.Z, MaxX.Z)	
		return CFrame.new(X, 1, Z)
	end
	
end


RunService.RenderStepped:Connect(function(Delta)
	local KeyW = Keys.W and Speed * Delta
	local KeyA = Keys.A and -Speed * Delta
	local KeyS = Keys.S and -Speed * Delta 
	local KeyD = Keys.D and Speed * Delta
	
	MovePart.CFrame = MovePart.CFrame * CFrame.new((KeyW or KeyS), 0, (KeyA or KeyD))
	MovePart.CFrame = ClampBounds(MovePart.Position)
	
	
end)

I got it to work!

1 Like