Custom camera keeps shaking

Im making a top down angled camera, like in retail tycoon 2. I havent came far yet, but ive got a issue that ive been stuck on for hours now, and i cant seem to figure out whats going wrong. The camera starts to shake once the player tries to move it.

-- Code is ran every frame in a bindtorenderstep with Camera priority

	local CameraLocalCFrame = Plot.WorldPivot:ToObjectSpace(Camera.CFrame)	
	
	-- Handle Inputs
	if UIS:IsKeyDown(Enum.KeyCode.W) then
		CameraVelocity = Camera.CFrame.LookVector
	end

	-- Handle Camera
	
	local NewCameraLocalPosition = CameraLocalCFrame.Position+CameraVelocity
	local NewCameraLocalRotation = Vector3.new(CameraLocalCFrame:ToEulerAnglesYXZ())+CameraAngularVelocity
	CameraVelocity = CameraVelocity*0.9
	CameraAngularVelocity = CameraAngularVelocity*0.9
	
	print(CameraVelocity)
	
	local CameraWorldCFrame = (CFrame.new(NewCameraLocalPosition.X, 20, NewCameraLocalPosition.Z)):ToWorldSpace(Plot.WorldPivot) --*CFrame.Angles(NewCameraLocalRotation.X, NewCameraLocalRotation.Y, math.rad(-30))
	Camera.CFrame = CameraWorldCFrame


In the video i pressed W for like 0.5 seconds, then stopped and did nothing. As soon as i stopped holding, the velocity started going down to 0,0,0. And it still continued shaking even after reaching 0,0,0.

Another issue is that the camera doesnt go that far forward for some reason. And i got no idea why that is happening.

Im suspecting both of these issues are due to the ToObjectSpace operations im doing. But i dont know how to fix it, i still want to use object space when dealing with the camera.

2 Likes

Is it connected to a player

if so then every frame when you reset its position you have to offset it from the player using the z vector of toObjectSpace

If not then you can’t reset the cameras position every time you have to keep its position and add onto that by what you want to move

1 Like

By “connected to a player” i assume you mean if the camera is set to scriptable or not. The camera is set to scriptable.

I don’t really understand what you mean, but i can explain what im doing in more detail.
The camera is movable using your keyboard, i only got W as a key rn when testing.

And im using object space to make it easier to position the camera relative to the players plot. So if NewCameraLocalPosition is 0,20,0, then the camera would be 20 studs above the plot.

CameraVelocity is a way for me to easily set the velocity of the camera, so if its 2,0,0 then every frame the camera would move 2 studs on the x axis

it may have delays from IO like, outputting, and checking the input

I would instead do it like this

camCFrame = something

on input
   adjust camCFrame

on renderstep
   set camera's cframe to camCFrame

also, as the other replies say, yea, you need to be in cameratype=scriptable

1 Like

I have cameratype set to scriptable as mentioned in my reply.

I thought about doing it the way you say, but i prefer doing

CameraVelocity = something

on input
   adjust CameraVelocity

on renderstep
   set camera cframe to camera cframe+CameraVelocity
   reduce CameraVelocity

using velocity makes it easy to have a smooth camera movement

oh, didnt see that you were working with velocity

try checking if the cameratype is scriptable inside the renderstep
sometimes, people set the cameratype too early and then it gets overwritten by the default camera script loading and changing it again

i tried that and it did no difference

it doesnt look like you’re using velocity correctly
velocity equations are like:
[new position] = [old position] + ( [velocity] * [change in time] )
and you’re doing:
[new position] = [old position] + [velocity]

i forgot about that, but it shouldnt have anything to do with this, and i was running at a smooth 60 fps.

try doing :ToWorldSpace(local cframe) to the pivot rather than :ToWorldSpace(pivot) to the local cframe

I usually just use :Inverse() and multiplying rather than those functions, so I’m not sure if that’s backwards

1 Like

try checking the pivot, maybe it’s changing every few frames by a few studs?

Do you have a player model that the camera follows?

No, as you can see in the code, its using a custom movement system. And as ive mentioned several times now, the camera is set to scriptable

Oh that fixed it, i changed from doing ToWorldSpace on the local cframe, to doing it on the pivot cframe.

Thank you so much

1 Like