Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

how to get the mouse position on a 2d plane in a 3d space roblox?

Asked by 3 years ago

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

0
This is just a suggestion (I don’t know if it will work) but maybe have a part that goes to the mouses the position in the 3d workspace, and then the camera set on that part. 1JBird1 64 — 3y
0
i thought of that but i was like i still have to get the mouse position in 3d space which is what im having a problem with proROBLOXkiller5 112 — 3y
0
I’ve edited Speedmask 661 — 2y

1 answer

Log in to vote
1
Answered by
Speedmask 661 Moderation Voter
3 years ago
Edited 2 years ago

use WorldToScreenPoint()

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.

0
I didn't really explain that much so let me know if you don't get something Speedmask 661 — 3y
0
i dont understand what WorldToScreenPoint() do proROBLOXkiller5 112 — 2y
0
nor what the calculations you put in are for proROBLOXkiller5 112 — 2y
0
by the way is mouse.Hit.p only bound to everytime you click your mouse or does it just dont care and will return a vector3 if you bind it to a loop proROBLOXkiller5 112 — 2y
View all comments (3 more)
0
nevermind idk how to stop mouse.Hit.p from positioning the part's position above the part itself proROBLOXkiller5 112 — 2y
0
just make a post here dedicated to explaining what your code does cuz i cant understand any of it proROBLOXkiller5 112 — 2y
0
yes this is exactly what i want proROBLOXkiller5 112 — 2y
Ad

Answer this question