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:


local function ClampBounds(HitPos)
	local ModelSize = (,math.abs(MovePart.Size.Y), math.abs(MovePart.Size.Z)))/2
	local MinX, MaxX 
	for _, Plot in ipairs(Plots) do
		local PlotSize = (,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, 1, Z)

	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 * or KeyS), 0, (KeyA or KeyD))
	MovePart.CFrame = ClampBounds(MovePart.Position)

I got it to work!

1 Like