Hello everyone,
I am currently working on a project that requires positioning dummy models (e.g., pets) in front of the camera at various positions. The challenge is to have them face the camera and maintain a fixed orientation regardless of camera movement, similar to the behavior of ViewportFrames, but without using ViewportFrames due to quality considerations.
The complexity arises when there are multiple dummy models on the screen at the same time, potentially up to 8. Each model should maintain its position and orientation relative to the camera, whether it’s placed at the center or at the corners of the screen. I have attempted to calculate the positions and orientations using CFrame and vector mathematics, but I have been struggling to achieve the desired behavior.
Here’s a snippet of the code I’ve been working with:
local ray = camera:ViewportPointToRay(cfg.RelativePosition.X, cfg.RelativePosition.Y)
local direction = ray.Direction.unit
local worldSpacePosition
if cfg.Offset > 0 then
worldSpacePosition = CFrame.new(camera.CFrame.Position + (direction * cfg.Offset), camera.CFrame.Position)
else
worldSpacePosition = CFrame.new(camera.CFrame.Position + direction , camera.CFrame.Position)
end
if cfg.Object:IsA("Model") then
cfg.Object:PivotTo(worldSpacePosition)
else
cfg.Object.CFrame = worldSpacePosition
end
Here how it looks rn and the issue i’m facing when moving the camera
https://gyazo.com/6794553e59ca898ec4e93be4de9056af
Some of the pet at the left and right start moving when they shouldn’t, they should all be in the same orientation like the center one