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