Godot Version: v4.3.stable.official [77dcf97d8]
When I look up the term, the internet says I’m looking for intelligent zooming. I am still quite new to GDscript, otherwise I think I’d be able to do something with a video I’ve found where the guy does what I’m looking for but in 3d.
I have zoom working, it increases and decreases the zoom when scrolling the mouse wheel, but I’d like the zoom to be relative to the cursor’s current location, like when you’re using a graphic program, or other such programs.
If it helps I’ll put the script I use for zooming, and maybe if someone has something I can plug in to make it do, that’d be nice. I’d also appreciate a description of why the code works the way it does, as I’m always interested in that sort of thing. So far I’ve been able to pick up most things by watching people use the code, and then sort of fitting it to my purpose when needed, but in this case the only person I’ve seen doing exactly what I’m looking for is doing it in 3d, and in a video from 4 years ago.
This is my full camera script, so far, I don’t know why it’s not all in the box:
Edit: added that bit about the box, but also I wanted to let you know my camera is in its own node, and works great for what I can get it to do so far, just by dragging it into the node I need it and using it. I have a canvas layer that I attatch to it, for the esc menu, which comes up at the top right, but only when it’s full screen in the proper resolution (one day I’ll find the video I found that would let me fix that) and I have no canvas layer that it’s nested into or anything, it’s just child to the main node2d that runs the main scene. I had someone ask for an example of what I’m looking for, so I guess instead of putting a video I invite you to open godot right now, get into a scene with at least one image, then put your mouse on one side of the image, zoom in, then put it on the other side of the image and zoom out. THAT is what I’m looking to get going here. Nothing too extreme, just the little camera panning that happens when you zoom. The direct in and out that I have now is only acceptable for when I pin it to a 2d character boi, which it works great for btw. (I have seriously considered somehow making a 2d npc that follows my mouse, but only when I zoom, that I can then have hover on the camera, so it stays relative somehow to the camera, but also not? it’d be a very stupid way to solve this, so I don’t want that. he’d also be invisible!)
Edit 2: Here is the script I’m using now. it’s a bit of a frankensmonster of different things I’ve grouped together, but it does what I want to do, even if it’s not quite as smooth as I’d like yet.
Edit 3: Figured out how to put code in!
had to fix the stuff with astrisks not showing up ><
extends Camera2D
var zoomSpd: float = 0.05*zoom.y
var Minzoom: float = 0.1
var Maxzoom: float = 1.0
var dragSen: float = 1.0
var spd = zoomSpd*3.5*zoom.y*zoom.y ##comfortable zoom/pan speed still a bit jagged.
func _input(event):
var mouse_position = get_global_mouse_position()
var mouse_delta = mouse_position - global_position
if event is InputEventMouseMotion and Input.is_mouse_button_pressed(MOUSE_BUTTON_RIGHT):
position -= event.relative * (dragSen/zoom.y)#pretty sure this last bit is the way you get it to scale.
if event is InputEventMouseButton: #for zooming
if event.button_index == MOUSE_BUTTON_WHEEL_UP and zoom.y!=Maxzoom:
zoom += Vector2(zoomSpd,zoomSpd)
position += mouse_delta * spd
elif event.button_index == MOUSE_BUTTON_WHEEL_DOWN:
zoom -= Vector2(zoomSpd,zoomSpd)
if zoom.y>Minzoom:
position -= mouse_delta * spd
zoom = clamp(zoom, Vector2(Minzoom, Minzoom), Vector2(Maxzoom, Maxzoom)) #Limits the zooming