Annoying CFrame.fromMatrix complication

Hello, I’ve been trying to make slow view bobbing on the camera for a title screen, but my console gets spammed with errors and my camera stays in one spot, heres my code:

local RunService = game:GetService("RunService")
local TweenService = game:GetService("TweenService")
local Camera = workspace.Camera

local lockPart = workspace.CameraPart

wait(0.2)
Camera.CameraType = Enum.CameraType.Scriptable	
Camera.CFrame = lockPart.CFrame
wait(0.5)

RunService.Heartbeat:Connect(function()
local Tween = TweenService:Create(Camera,TweenInfo.new(5,Enum.EasingStyle.Sine),{
CFrame = CFrame.fromMatrix(
Vector3.new(
lockPart.Position.X+math.random(-1,1),
lockPart.Position.Y+math.random(-1,1),
lockPart.Position.Z+math.random(-1,1)
),
Vector3.new(
lockPart.CFrame.LookVector.X+math.random(-1,1),
lockPart.CFrame.LookVector.Y+math.random(-1,1),
lockPart.CFrame.LookVector.Z+math.random(-1,1)
)
)
})
	Tween:Play()
	repeat wait() until Tween.Completed
end)

The error I get:

 15:51:10.293 - Players.DokuDako.PlayerGui.LocalScript:23: bad argument #3 (Vector3 expected, got no value)
15:51:10.294 - Stack Begin
15:51:10.294 - Script 'Players.DokuDako.PlayerGui.LocalScript', Line 23
15:51:10.295 - Stack End

I’ve been trying to look up info from other people on the developer forum and developer hub, but couldn’t really find any info on this.
Heres a video showcasing the bug:


If anyone can help, thanks!

1 Like

The only wrong thing, that I can see now is just this part.
Put there local Camera = workspace.CurrentCamera instead, but to be honest, I don’t think this will absolutly solve the problem.

1 Like

It didn’t really solve the problem, but thanks for the small suggestion though!

Have you tried CFrame = CFrame.new(
?

The reason it isn’t working is because you are not providing the required third vector for fromMatrix to work.

You need at least three arguments. Fourth is optional.

Didn’t I apply the Z vector for the orientation? Whats going on?

Vector3.new(
lockPart.CFrame.LookVector.X+math.random(-1,1),
lockPart.CFrame.LookVector.Y+math.random(-1,1),
lockPart.CFrame.LookVector.Z+math.random(-1,1)
)

Ah, just saw that quick edit there. I’m not really that advanced in Maths but I’ll try the vx:Cross(vy).Unit and try to notify you when it worked.

I don’t think you should be using fromMatrix for this. If you are trying to create camera bobbing, there is no need for fromMatrix.

1 Like

Oh, alright then.
I might as well use some other CFrames for that.

You should really only be using CFrame.new for that, since all you need to change is position.

So for bobbing, I would just have a position for wherever the camera starts. From there, you can subtract and add however much you want the camera to rise and fall from the Y axis of that position.

IE: If my camera starts at Vector3.new(0,5,0), and I want it to fall and rise 2 studs, then I’ll store four variables.

StudDifference = 2
InitialCameraPosition = Camera.CFrame.Position ( Vector3.new(0,5,0) )

HighExtreme = InitialCameraPosition + Vector3.new(0,StudDifference,0)
LowExtreme = InitialCameraPosition - Vector3.new(0,StudDifference,0)

Then you can just tween to a CFrame that has one or the other value at a time as its position.

Tween to CFrame.new(HighExtreme) , then Tween to CFrame.new(LowExtreme) , and repeat.

This is the general idea. Depending on what you are doing, you will need to swap some things out here and there to make it work correctly for the scenario.

1 Like

Understood, thank you for your help!