How do i make sure a player can't move after touching the edge of the screen

hey viewer! i’m working on a 2D fighting game inspired by the Street Fighter series. one of the staples of Street Fighter is the camera, which prevents players from moving backwards after touching the edge of the screen.


as you can see, both player’s characters are halfway across the edge of the screen, which i don’t want. (no, this isn’t zoomed in)


both player’s are directly in view of the camera (which is what i want), they can move as long as they don’t move past the edge of the screen.

i’ve written up some code that detects if a player, opponent or both player and opponent are “out of bounds” (see below) but i’m struggling on preventing the players from moving backwards once they step out of bounds.

i’ve tried the following:

  • anchoring the player
  • creating a wall slightly behind the player, and destroying it briefly afterwards

i used :FireServer() for the above methods; none of them worked successfully

code
local function CreateDistanceFromVector(vector, rightVector, leftVector)
	local distances	= { 
		left = (vector - leftVector).Magnitude;
		right = (vector - rightVector).Magnitude
	}

	return distances
end

local function CheckDistance(distance)
-- MAX_DISTANCE in this case is 100 pixels
-- distance arguement is the table created from CreateDistanceFromVector()

	if (distance.left <= MAX_DISTANCE) or (distance.right <= MAX_DISTANCE) then
		return true
	end

	return false
end


function CameraController:GetEntityDistanceFromCamera(root)

	local cameraToRootVector = self.Camera:WorldToScreenPoint(root.Position)
	local rightVector =  Vector2.new(self.Camera.ViewportSize.X, 0)
	local leftVector = Vector2.new()

	local newVector	= Vector2.new(math.clamp(cameraToRootVector.X, 0, rightVector.X), 0)

	local distances = CreateDistanceFromVector(newVector, rightVector, leftVector)
	
	return distances
end

function CameraController:CheckOutOfBounds()
	local OPPONENT_OUT_OF_BOUNDS = CheckDistance(self:GetEntityDistanceFromCamera(self.OpponentRoot))
	local PLAYER_OUT_OF_BOUNDS   = CheckDistance(self:GetEntityDistanceFromCamera(self.PlayerRoot))

	if (PLAYER_OUT_OF_BOUNDS and OPPONENT_OUT_OF_BOUNDS) then
		self.IsOutOfBounds = true
	elseif PLAYER_OUT_OF_BOUNDS then
		self.IsOutOfBounds = true
	elseif OPPONENT_OUT_OF_BOUNDS then
		self.IsOutOfBounds = true
	else
		self.IsOutOfBounds = false
	end
end

How about setting walk speed to 0?

how would they move out then? they would be stuck there

Just add invisible parts to block off the spot they can’t walk to

2 Likes

The bound would have to be either on the left or right of the player, so if for example the player is on the left bound, when the player hits “D” or the right arrow, the speed is restored.

wouldn’t it be easier to just make it so the “A” key is disabled in the left bound instead of messing with walkspeed

You don’t even need to do that, literally invisible parts blocking off the area will work

1 Like

He tried that and it didn’t work.

ik that I was just saying that walkspeed is dumb in this case

How did it not work? Invisible parts that you can collide with and that are anchored won’t allow the player to walk any further. It’s simple common sense.

1 Like

No clue but he said this here in the original post.

Just keep the part there, just invisible no need to create it and destroy it programmatically

1 Like

He tried making it there when the player walks too far, and then destroy itself after. All he has to do is put a part there in studio, no need to do it with a script.

1 Like

AH I know why, it’s because the bounds depends on what the screen sees so the bounds will constantly change.

Source:

even if i do this, the player will still be out of bounds regardless if they touch the part or not

No it’s a fixed camera, so no matter where they move the camera will stay static

2 Likes

I don’t understand the issue. Can you elaborate?

No, they won’t. Have a static part that does not move or change, set it to the exact position that defines the bounds and then they can’t move there. It’s literally that simple.

1 Like

i really wasn’t hopin’ i’d use static parts, but i’d finally made it work with my camera so i’m satisfied. thanks for the help everyone!

1 Like