Problems with changing a Camera2D's offset based off of local mouse position. (Accidental Dupe. Pls delete this one.)

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By PSISoft

So im making a top down shooter and Im trying to make the player’s camera move with mouse movement. For this I tried $Camera2D.offset = get_local_mouse_position() and it gave results closest to my desired effect, but whenever I move my mouse to the left side of the screen, the camera gets pushed to the right which is what I dont want. I tried many other methods but changing the camera’s offset is best for my game. Is there a way to fix this so that way the camera goes to the left as it should.

You can hide your question

exuin | 2021-03-26 03:06

:bust_in_silhouette: Reply From: Mrpaolosarino

Do you need a camera that follows?

$Camera2D.position = linear_interpolate(get_global_mouse_position()

If not then can you elaborate more of what your desired effect so we can understand

Apologies for being a little less descriptive. Ill edit the post have a bit more detail on the post. But also, there is no linear_interpolate() function in godot as default or defined in my code. Can you give me a better solution?

PSISoft | 2021-03-26 02:50

Ok this is much more easier:

$Camera.position = get_global_mouse_position()

and then in your camera, go to Smoothing and turn it on.
it’s up you whenever you’ll experiment on this one


I tried it in my test and if you want a camera that follows player at the same time pan in your cursor a little bit

change the code above to :

$Camera.position = get_local_mouse_position()

Mrpaolosarino | 2021-03-26 03:06

Thanks! Youre being a big help, but one more thing. Is there a way to set up a limit for how far the camera can go towards the mouse until stopping? When ever I do play around with the second code snippit you gave me, the camera goes way to far for my liking, and always want to make it where the players stays in view with a slight pan based off of local mouse position.

PSISoft | 2021-03-26 03:16

Lol brother, you must learn how to solve your own problem as a programmer. Either way, replace get_local_mouse_position to $Camera2D.get_local_mouse_position

Mrpaolosarino | 2021-03-26 03:21

No worries brother, Im not gonna be completely dependent on the internet. I only really try to use forums like these as a last resort when I cant find something out. But It works as expected. Thank you very much and have a great day/night.

PSISoft | 2021-03-26 03:27