Using TweenService to jerk camera rotation affecting camera's position as well

Hi, I’m making a camera shake effect for some guns using TweenService and I’ve come into a problem: My method affects the Camera’s position, which I do not want. What I want to do is to ONLY rotate the camera’s CFrame and leave the position unaffected.

Here’s the code sample below (X, Y, Z are all just angle values to shake the camera)

local TweenService = game:GetService("TweenService")
local Info = TweenInfo.new(0.075, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, true, 0)
game.ReplicatedStorage.RemoteEvents.CameraShake.OnClientEvent:Connect(function(X, Y, Z)
	local Goal = {}
	
	Goal.CFrame = workspace.CurrentCamera.CFrame * CFrame.Angles(X, Y, Z)
	local Tween = TweenService:Create(workspace.CurrentCamera, Info, Goal)
	Tween:Play()
end)

I’ve tried several solutions, but they don’t work. Here’s my changes and results:

Fix: Using the above code
Result: Rotation works perfectly, but the position gets offset for a moment (I’m using a First-Person ViewModel, so I think the Tween is what’s making it offset like that)

Fix: Changing Goal.CFrame to Goal.CFrame.Rotation and deleting workspace.CurrentCamera.CFrame so it’s only the CFrame.Angles part remaining.
Result: Gives an error stating ‘attempt to index nil with “Rotation”’. Apparently I can’t use that property of CFrame as a goal in a Tween array.

Fix: Everything same as code sample, but I only left the CFrame.Angles part alone.
Result: Camera’s position goes to the origin of the world, screwing up everything entirely.

I’m out of ideas, can someone help me?
Thanks

You could probably use something like this:

local Info = TweenInfo.new(0.075, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, true, 0)
local Goal = {Vector3.new(rotation values)}

local Tween = TweenService:Create(workspace.CurrentCamera.Rotation, Info, Goal)

That just gives me the error “unable to cast to dictionary” on the line where I create the tween.

Try this (I edited @BriefYayyayyay code)

local TweenService = game:GetService("TweenService")

local Info = TweenInfo.new(0.075, Enum.EasingStyle.Quad, Enum.EasingDirection.Out, 0, true, 0)
local Goal = {CFrame.Rotation = Vector3.new(rotation values)}

local Tween = TweenService:Create(workspace.CurrentCamera, Info, Goal)

image

Gives a syntax error. It disappeared when I put == instead of =, but then it gave me the same error, “unable to cast to dictionary”.

By the way, X, Y and Z are all math.rad(random number) values, rather than normal numbers (sorry if this is important information, I forgot to mention it earlier)

1 Like
Camera.CFrame = CFrame.new(Camera.CFrame.Position) * CFrame.fromOrientation(0, math.pi, 0) --Rotate camera 180 degrees around the Y-axis.

That doesn’t help, it just focuses the camera onto that specific rotational CFrame, plus the camera is still stuck when the tween plays.

Changing Camera Rotation WITHOUT Affecting Position

You may want to fix your title to accurately represent that.

2 Likes