` Hello. Thank you for making these forums available. I’m starting out making a side-scrolling open-world platformer. I was able to make the camera follow the player using a script (I heard it was better to do this in case you wanted the camera to do other things other than follow the player). I followed a tutorial that made the player the target of the camera in the inspector panel.
However, I also want the player of my game to be able to zoom into my player. Thus far I’ve been having great difficulty with this. I’ve figured out how to make the camera zoom–I have those set to I for zoom and O for zoom out and P for reset to the default zoom. I thought that setting the camera to follow the player would cause my zoom function to zoom to the center of the player node, but to my surprise it still zooms to the center of the viewport but at the position of the player.
Here is my present script.
extends Camera2D
@export var target: Node2D
# This script is a combination zoom function and follow player function.
# The values below are related to the zoom function.
const MIN_ZOOM = Vector2(1.0, 1.0)
const MAX_ZOOM = Vector2(2.0, 2.0)
const DEFAULT_ZOOM = Vector2(1.2, 1.2)
const ZOOM_SPEED = 1.0 # How fast it zooms per second
const RESET_SPEED = 5.0 # How fast it resets to default
var resetting_zoom := false
func _process(delta: float) -> void:
# --- Camera Follows Player: Player is set to target in inspector panel. ---
if target:
position = target.position
# --- Zoom Handling ---
var zoom_direction = 0.0
if Input.is_action_pressed("zoom_in"):
zoom_direction += 1.0
if Input.is_action_pressed("zoom_out"):
zoom_direction -= 1.0
if zoom_direction != 0.0:
resetting_zoom = false
var target_zoom = zoom + Vector2(zoom_direction, zoom_direction) * ZOOM_SPEED * delta
zoom = target_zoom.clamp(MIN_ZOOM, MAX_ZOOM)
elif resetting_zoom:
zoom = zoom.move_toward(DEFAULT_ZOOM, RESET_SPEED * delta)
if zoom == DEFAULT_ZOOM:
resetting_zoom = false
if Input.is_action_just_pressed("zoom_reset"):
resetting_zoom = true
`
I’m a novice programmer, getting help from LLMs to my dishonor. And even they can’t figure out how to make the camera zoom to the center of the player character.
If anyone can help I would appreciate it greatly, as this is one of the central functions I want to include in my game.
2D (and 3D…) objects in Godot have two position values you can play with: position, and global_position. The position is the object’s location relative to its parent. The global_position is its location relative to the world.
It can. I didn’t see you updated your code. There are no notifications when someone edits their post.
That’s up to you.
This statement is the center of your issue. You:
Are trying to use LLMs to get a quick answer which results in you not understanding what you are doing.
Seem to believe that LLMs are infallible.
LLMs suck at coding. A new study shows that they make coding take 20% longer for experienced programmers. Another study shows that on average they get answers wrong 25% of the time. LLM accuracy is even worse for Godot. It will give you code and answers from previous versions. It will also make up function names and tell you they exist when they don’t.
Whoever told you this was wrong. Make the Camera2D node a child of the player node and it’ll follow it around without any code. Then the only code you need is zoom code. It’ll stay centered on the player.
Making things more complicated to cover possible future scenarios is called future proofing. We all do it. It’s a bad habit. In Godot it’s much easier to add a second camera and just switch that to be the main camera than to move one camera around through code.
While this does depend on OP’s game, I’ve never made a game that looked even half-decent where the camera was fully snapped to the player - for the camera to look nice, it need some interpolation and to be biased toward points of interest. Sorry if this isn’t relevant, just wanted to say that moving a camera with code is probably a better solution.
Personally, for moving my Godot camera, I always use the offset property as for some reason global_position doesn’t affect the camera.
That comment is, in my opinion, you future proofing someone else’s code. They want a working zoom. I provided a simple option to get them going.
Depends on the style you’re going for. You ever played a platformer like that? I find it really annoying. Same with top-down RPGs. It’s great for RTSes or farming sims though.
It’s not that it isn’t relevant - it just isn’t very helpful IMO. If I were in your position and felt that strongly about it, I would provide the OP some code to help them - as that is what they asked for. They’ve already gotten the advice you are giving. That’s how they got here.
I briefly included some advice, but I’ll make it more prominent now:
I use this property to set the camera position and ignore all its built-in tools, directly setting the view position. (Click the docs for more info)
So if OP wants to zoom into the player, the camera needs to be directly centered on the player. To get that effect, you can interpolate the zoom and interpolate the position toward the player at the same time.
@ComicallyUnfunny I’ve never become proficient in any coding language. All I know are the basics. That there exists things like variables, strings, if and and or conditionals, classes, and whatnot. But as with a real language, it only sticks if you use it, and I’ve never used it enough for it to stick.
As for gd script, I’ve only done the learn gd script from zero course, but that doesn’t bridge the gap between learning the basics and exactly how to attach scripts to a node, and it doesn’t scratch the surface of all the nitty gritty details.
The problem with the Godot documentation is that it doesn’t guide you step by step either. It just basically tells you, “This concept exists.” It doesn’t tell you, “In order to make the camera zoom in on your player sprite, first you have to create a node, then create a child this or that node, then you have to attach this script to this node, then you press f5 to run your game.” Based on what I’ve seen, it’s nowhere near that detailed for a beginner. No “This is exactly how you zoom into your character sprite instead of the main screen.” Not to sound unappreciative of all the info those guys provided: I’m most appreciative. It’s just that I’ve always faced those kinds of gaps when learning how to code in any game. Some people have math/engineer/Doctor-Spock-logical brains and are naturals at this. I’m more of an artist type who happens to have a bit of a logical mind, which at least gives me fascinated with coding and inclined to learn how to do it once and for all–also I want to make a game badly enough to learn; but because I’m not a natural it takes me more effort to bridge the gap between documentation and exact application. And honestly at the moment I don’t know what the key is for me make coding stick and solve this problem. I’m essentially picking a direction I think will enable me to solve this problem and walking toward it, hoping that I guessed right. And right now my direction is search the web for gd tutorials that actually teach you the syntax required to apply an intended event to the godot engine. Here I go.
Gamedev.tv and Zenva both have good courses. There are also lots of pretty good YouTube tutorials out there, but I’d recommend GameDev.tv. They have a forum for you to ask questions, and moderators who actually to reply to questions in courses when you get stuck.
In response to learning how to code: there are thousands of tutorials out there for how to code. Brackeys has a tutorial that will have you with a working platformer in an hour if its all new to you.
Once its done, play with it. Tweak it. Try to add or change functionality. When you get stuck, reference the built in documentation for the node or function thats got you stuck. There are legitimate tutorials in there, and full breakdowns of using the code, its just a matter of taking the time to read it, try to implement it, fail, and try again with a hopefully better understanding. Google your problems with your godot version number, dig through old forum post, and follow along with tutorials. Do the grunt work and it will pay off. I spent two weeks doing that and i already feel more confident and see more avenues to learning more.
You can do this, and it will pay off. Dont be afraid to scrap something and start over! There’s no pressure, only learning.
Thanks for that. You spoke of the built-in documentation with more credit than I initially gave it. I assumed it would be as vague as the documentation but I’ll give it another look.
I can’t say theres a tutorial on every page, but there are many useful ones. Viewports, charavter bodies, signals, many of these have a tutorial. Leadning to read the documentation is daunting at first, but i think you’ll start to understand what’s being laid out and how with a bit of time reading it.