So I’m not quite sure how to explain this, but I’m trying to recreate this
As you can see when the opponent (CPU in this case or Juri) touches the edge of the screen it prevents the player from walking further back same goes for the opponent they also are unable to walk further back so my question is how can I check if a player is touching the edge of the screen/camera?
This is the code I have for my current camera not sure if this matters though.
--| Camera
local WorkspaceCam = workspace.Camera
local StageCam = workspace.Game.Stage.ImportantParts.Camera
local Distance = 15
local Xoffset = 0
local Yoffset = 2
--local Zoffset = 10
local HRPS = {}
for _,HRP in pairs(workspace:GetDescendants()) do
if HRP.Name == "HumanoidRootPart" then
table.insert(HRPS,HRP)
end
end
function CalculateAveragePosition()
local Total = Vector3.new()
for _,HRP in pairs(HRPS) do
Total += HRP.Position
end
return Total / #HRPS
end
--function CalculateAverageMagnitude()
-- local Total = 0
-- for _, HRP in pairs(HRPS) do
-- Total += (HRP.Position - StageCam.Position).Magnitude
-- end
-- return Total / #HRPS
--end
wait()
WorkspaceCam.CameraType = Enum.CameraType.Scriptable
Main.RunService.RenderStepped:Connect(function()
local AveragePos = CalculateAveragePosition()
--local AverageMagnitude = CalculateAverageMagnitude() + Zoffset
StageCam.Position = AveragePos
WorkspaceCam.CFrame = StageCam.CFrame * CFrame.new(Vector3.new(Xoffset,Yoffset,Distance--[[AverageMagnitude]]))
end)
local humanoidRootPart = character:WaitForChild('HumanoidRootPart')
local vector, isOnScreen = workspace.CurrentCamera:WorldToScreenPoint(humanoidRootPart.Position)
local vector = Vector2.new(vector.X, 0) -- since the function returns a vector3, we want to put it into a vector2, and the Y coordinate is not relevant in our case as we want to calculate the distance from the edge
local distanceFromLeft = (vector - Vector2.new(0,0)).Magnitude
print(distanceFromLeft)
local distanceFromRight = (vector - Vector2.new(workspace.CurrentCamera.ViewportSize.X,0)).Magnitude
print(distanceFromRight)
Ight so this works however how would I tell exactly when the character touches the edge? Would I have to set some values and do something like this? I thought I would use IsOnScreen, but that wouldn’t work because I want the characters to be on screen still so I tried doing something like this.
Main.RunService.RenderStepped:Connect(function()
local HRP = workspace:WaitForChild("Opponent").HumanoidRootPart
local Vector, IsOnScreen = workspace.CurrentCamera:WorldToScreenPoint(HRP.Position)
local Vector = Vector2.new(Vector.X, 0) -- since the function returns a vector3, we want to put it into a vector2, and the Y coordinate is not relevant in our case as we want to calculate the distance from the edge
local DistanceFromLeft = (Vector - Vector2.new(0,0)).Magnitude
--print("Left"..DistanceFromLeft)
local DistanceFromRight = (Vector - Vector2.new(workspace.CurrentCamera.ViewportSize.X,0)).Magnitude
--print("Right "..DistanceFromRight)
if math.floor(DistanceFromRight) == 97 then
warn("TEST")
end
end)
``
Hmm, you’d probably want to create a connection that checks the distance at each render step.
Your function should work, the only thing is that the distance would be in pixels. You’d also want to check if the magnitude is equal to or less than 97, not if the distance is equal to 97. There might be a frame that skips over when the distance is equal to 97 which is why you’d want to use <= as opposed to ==.
if math.floor(DistanceFromRight) <= 97 then
warn("TEST")
end
Ah ight thanks a lot! My final question though will this work from anywhere on the screen? if DistanceFromRight <= 97? Or am I gonna have to get new values?
The if statement will work if the player is on the screen and the distance is less than or equal to 97, but if the player somehow goes over 97 pixels off of the screen, it won’t work properly. You’d probably want to clamp the X coordinate of the vector from 0 to the viewportsize.