im making a 2d game where if you move your mouse to the edgy of the screen your camera will go with it and stops until the players character is on the other side of the edgy
the way im making this is calculate the mid point position of the mouse and the humanoidrootpart constantly every frame and set the camera cframe to the mid point of those positions(humanoid root part and mouse position)
so far i have had no luck making this work the game either just dont update the mouse position constantly and just returns the same position over and over or its a mouse.hit function that returns a position when you press the mouse which doesnt help at all
this is the code i used
local Camera = workspace.Camera local ReplicatedStorage = game:GetService("ReplicatedStorage") local Mouse = require(ReplicatedStorage:WaitForChild("Mouse")) local MousePosition = Mouse.GetPosition() Camera.FieldOfView = 1 local HumanoidRootPart = script.Parent.HumanoidRootPart local RunService = game:GetService("RunService") RunService.RenderStepped:Connect(function() Camera.CFrame = HumanoidRootPart.CFrame + Vector3.new(0,0,3000) print(MousePosition) end)
plus me not having very much experience with the mouse doesnt help at all
local PERCENT_LEFT, PERCENT_RIGHT = 15, 15 -- percent of screen to move right and stop moving local CAM_SPEED = 1 -- camera movement in studs per second local DIRECTION = camera.CFrame.RightVector -- axis camera moves on local boundLeft = mouse.ViewSize.X * (PERCENT_RIGHT / 100) local boundRight = mouse.ViewSize.X * (100 - PERCENT_RIGHT / 100) RunService:BindToRenderStep("PanCam", 200, function(delta) if mouse.X > boundRight and camera:WorldToScreenPoint(character.PrimaryPart.Position) > boundLeft then camera.Position = camera.Position + DIRECTION * CAM_SPEED * delta end end)
edit: okay, so I’m not even sure if I did you were trying to do, looking back at this. please tell me if this is what you want:
when the player moves the mouse to the edge of the screen, the camera moves that way until the player is almost off of the screen again but then it stops. correct? the part I wrote only does this going rightwards but you can extend this by doing the same thing 4 times for 4 directions.
what WorldToScreenPoint does is exactly what it sounds like. it gets the x and y coordinates of of a position how it appears on the screen. when the mouse is detected at the rightmost 15% of the screen, the camera moves right. however, when the player is at the leftmost 15%, the camera will stop moving no matter what. using that function I am checking what position on the screen the character is to make sure they do not go offscreen.