Creating a parallax effect

I’m trying to create a parallax effect for a menu sequence in my game. I’ve have a (very basic) script that seems to work just fine, but there’s just one problem. The whole move the camera when you move the mouse thing works just fine but the problem is that I can’t choose where it starts from. It always seems to start it at the workspace’s origin no matter what I try. Here’s a video showing what happens. (I’m trying to get it to be positioned at this statue of a rock (i know) but it always ends up making it around 0, 0, 0 which is not very interesting you can see that I tried to position the camera using camera.CFrame first before I connected the mouse.Move function but to no avail)

Oh and here's the code

local camera = game.Workspace:WaitForChild("Camera")
wait(1)

camera.CameraType = Enum.CameraType.Scriptable
camera.CFrame = game.Workspace.RockCFrame.CFrame

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Move:Connect(function()
	camera.CFrame = CFrame.new(mouse.UnitRay.Direction.X, mouse.UnitRay.Direction.Y, mouse.UnitRay.Direction.Z)
end)

Thanks for the help!

3 Likes
local _desiredPos = workspace.DesiredPos

local _cam = workspace.CurrentCamera

_cam.CameraType = Enum.CameraType.Scriptable

local mouse = game.Players.LocalPlayer:GetMouse()

mouse.Move:Connect(function()

_cam.CFrame = CFrame.new(mouse.UnitRay.Direction.X, mouse.UnitRay.Direction.Y, mouse.UnitRay.Direction.Z) + Vector3.new(_desiredPos.Position.X, _desiredPos.Position.Y, _desiredPos.Position.Z)

end)

I found this solution working just adding + Vector3.new(_desiredPos.Position.X, _desiredPos.Position.Y, _desiredPos.Position.Z), the problem is in the UnitRay, its starts from zero and this zero’s also applies to camera position (kinda complicated). You can also multiply this by CFrame.Angles to correct the rotation of camera if its look backwards.

3 Likes

Alright I’ll try this thanks for the help!

It worked! Just… the camera is facing the wrong way. It’s okay I’m sure I’ll find a workaround!

use CFrame.Angles(0, DesiredAngle, 0):

_cam.CFrame = CFrame.new(mouse.UnitRay.Direction.X, mouse.UnitRay.Direction.Y, mouse.UnitRay.Direction.Z) * CFrame.Angles(0, math.rad(Angle u want), 0) + Vector3.new(_desiredPos.Position.X, _desiredPos.Position.Y, _desiredPos.Position.Z)

2 Likes