Hey, I am having a bit of trouble with the WorldToViewportPoint function of the camera.
I want to be able to move the camera around a model and have a red square over the
same part no matter how much i rotate the model. Currently it doesn’t position the
red box where i want it to and i don’t know if this is something that i am doing wrong
or if its a mistake with the function
Here is the code
function PositionPointer()
local Vector, OnScreen = Cam:WorldToViewportPoint(Scene.Level2.Window1.Position)
Pointer.Position = UDim2.new(Vector.X,0, Vector.Y, 0)
end
function UpdateCamPos()
Cam.CFrame = Scene.Plot1.CFrame
* CFrame.fromEulerAnglesYXZ(math.rad(LastRot.Y), math.rad(-LastRot.X), 0)
* CFrame.new(0, 3, Distance)
PositionPointer()
end
Here is a picture that shows where i want the marker to be compared to where it is
(I want the red frame in the center the blue part)
Here is the current layout plus the Properties
ViewportFrame:
I had the very same issue when trying to use these methods inside a ViewportFrame, and to my knowledge, after multiple attempts at solving the problem, you simply cannot use these methods in a viewportframe. It’s like their functionality breaks down once you try to use them outside of workspace
Your reply sparked an idea in my mind, it is a bit hacky bit it works like i want it to.
Steps:
1. Get the offset from the camera to the part in the viewport
2. Translate that to a world position using the current cam
3. Do the worldToViewportPoint with the new world position and currentcamera
function PositionPointer()
local Offset = Cam.CFrame:PointToObjectSpace(Scene.Level2.Window1.Position)
local WorldSpace = CurrentCam.CFrame:PointToWorldSpace(Offset)
local Vector, OnScreen = CurrentCam:WorldToViewportPoint(WorldSpace)
Pointer.Position = UDim2.new(0,Vector.X, 0, Vector.Y)
end
function UpdateCamPos()
Cam.CFrame = Scene.Plot1.CFrame
* CFrame.fromEulerAnglesYXZ(math.rad(LastRot.Y), math.rad(-LastRot.X), 0)
* CFrame.new(0, 3, Distance)
PositionPointer()
end
ps i have to do some more calculations if i want to use another viewport size