Topdown Camera Movement Issue

Im making a top down camera system, with movement using the mouse’s movement.

It uses a part that moves freely in the workspace. The camera is binded by RunService so that it is always moving with the part.

Mouse movement is used in ContextActionService, when the mouse is held, the camera moves.

Unfortunately, movement is jittery. (Most likely that the movement is locked to left and right and such)
I want it so the camera can move smoothly.

For Reference:

What I have right now:

Code:

if InputState == Enum.UserInputState.Change then

			if Mouseheld then

				local movespeed = 50
				local Left = Player_Camera.Position + Vector3.new(-movespeed, 0, 0)
				local Right = Player_Camera.Position + Vector3.new(movespeed, 0, 0)
				local Up = Player_Camera.Position + Vector3.new(0, 0, -movespeed)
				local Down = Player_Camera.Position + Vector3.new(0, 0, movespeed)
				if math.abs(Mouse.X - lastMouseX) > math.abs(Mouse.Y - lastMouseY)then
					--Player_Camera.Position = Player_Camera.Position + Vector3.new(-movespeed, 0, 0)

					if Mouse.X - lastMouseX > 0 then
						Player_Camera.WorldPosition = Player_Camera.WorldPosition:Lerp(Left, .1)
					else
						Player_Camera.WorldPosition = Player_Camera.WorldPosition:Lerp(Right, .1)
					end
				else
					if Mouse.Y - lastMouseY > 0 then
						Player_Camera.WorldPosition = Player_Camera.WorldPosition:Lerp(Up, .1)
					else
						Player_Camera.WorldPosition = Player_Camera.WorldPosition:Lerp(Down, .1)
					end
				end
				
				lastMouseX, lastMouseY = Mouse.X, Mouse.Y
			end

		end
2 Likes

Are you trying to replicate the same camera movement in the first video?(The one where the camera orbits around the globe)

Or is it top down as you said, RTS style camera movement?

As for the jittery movement you mentioned, it seems like you forgot to multiply the up/down (or left/right vectors, whichever) vectors when moving the camera which is why its only moving in one direction.

I’m doing a top down camera system

Can you elaborate?

Sure, lets say you want to move the camera towards the bottom right.
You need to multiply both the down vector and the right vectors.
Here’s an example:

In order to implement this, we first have to breakdown your code:

if math.abs(Mouse.X - lastMouseX) > math.abs(Mouse.Y - lastMouseY) then
	code....
else
	code...
end

This checks only if the x axis is greater than y, meaning it can only move in one direction as I’ve said in my previous post. Now in the image above it includes both the X and Y axis together. So to fix this, we do the following:

-- check if X is positive and Y is negative
if math.abs(Mouse.X - lastMouseX) > 0 and math.abs(Mouse.Y - lastMouseY) < 0 then
	Player_Camera.WorldPosition = Player_Camera.WorldPosition:Lerp(Left, .1)
	Player_Camera.WorldPosition = Player_Camera.WorldPosition:Lerp(Down, .1)
end

You could try to repeat this if statement for each quadrant (all 4 corners) and it will work, but this will be some issues.

The issue being your camera movement is constant, meaning that it can only go exactly 45 degrees in every direction. (because you are increasing it by 50 each time which is the movespeed)

Now this is where I can’t fix this issue since I’m not an expert in camera movement
and that it’s a very complicated topic if you want to have smooth movements that covers all angles of direction.

You should try looking up Linear Velocity to solve this problem.

Here is the link to Linear Velocity documentation

You could try to use Linear Velocity to get slow camera movement and fast camera movement depending on whether you move the mouse fast or slow.
You can also use this to cover the angles depending on the movespeed of the x and y axis.

If you want to create an attachment to the camera then you have to create an invisible part(Just name the part Camera to make it easier) and put the attachment there, then copy the part’s CFrame over to the camera.

I will say that you might have to completely rewrite your code if you want to achieve this effect.

To finish off, I apologise if this was not the solution you were looking for but hopefully this helps.

1 Like