Тёмный

Drag Camera: Godot Guide 

Bramwell
Подписаться 18 тыс.
Просмотров 16 тыс.
50% 1

Try the code in action here! bramreth.itch.io/panning-cam
The source code and fully-fledged guides are also available for download on itch.
This guide teaches you how to pan a 2D camera and a selection of essential Godot skills.
Social links:
Twitter: / bramreth
Discord: discord.gg/YjhM98xZkY
Buy me a coffee: www.buymeacoffee.com/bram
My games: bramwell.itch.io/

Опубликовано:

 

22 янв 2022

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 59   
@kebstrr
@kebstrr Месяц назад
I can't tell you how long I have searched for such a simple functionality. 1:09 really was my AHA moment. Thank you for the video!
@diegosolis9681
@diegosolis9681 Год назад
Concise and to the point. I freaking love you dude!
@MisterBrown94
@MisterBrown94 2 года назад
Clean, understandable and well-presented. Good job, that's an earned donation :)
@glabados8
@glabados8 Год назад
This works fantastically except for the movement speed being adjusted by the zoom. It seems to be the opposite of what it should be so if you're zoomed in it pans too fast and if you're zoomed out it pans way too slow. I fixed it by dividing by zoom instead of multiplying.
@AdaLollA
@AdaLollA Год назад
big pp man
@snarekeeper8053
@snarekeeper8053 9 месяцев назад
(At least for Godot 4) You have to do: position -= event.relative * zoom.Inverse() (or zoom.inverse() i am not sure if it is capitalized in GDScript, I rewrote it to C#)
@4dragons632
@4dragons632 11 месяцев назад
This worked brilliantly, thankyou for the tutorial. I also had an issue at first, but I swapped from using _unhandled_input to using _input and it worked perfectly after that.
@doiros
@doiros 10 месяцев назад
I tried it, it still doesn't work, could you help?
@4dragons632
@4dragons632 10 месяцев назад
@@doiros What sort of error code or undesired behaviour are you getting?
@doiros
@doiros 10 месяцев назад
@@4dragons632 Literally no error code, it just doesn't work.
@4dragons632
@4dragons632 10 месяцев назад
@@doiros Try commenting out large chunks of it, and put in lots of print('name of what you're trying to do here') commands. Then check which of those commands display and see if you can get it to do _anything_ at all.
@slecornu
@slecornu 2 года назад
@samirsuleymanov2099
@samirsuleymanov2099 Год назад
Thank you! Helped a lot.
@nowthatsgaming5447
@nowthatsgaming5447 10 месяцев назад
This code works on godot 4.1 extends Camera2D var zoom_min = Vector2(.2,.2) var zoom_max = Vector2(2,2) var zoom_speed = Vector2(.2,.2) func _input(event): if event is InputEventMouseButton: if event.is_pressed(): if event.button_index == MOUSE_BUTTON_WHEEL_DOWN: if zoom > zoom_min: zoom -= zoom_speed if event.button_index == MOUSE_BUTTON_WHEEL_UP: if zoom < zoom_max: zoom += zoom_speed pass func _unhandled_input(event): if event is InputEventMouseMotion: if event.button_mask == MOUSE_BUTTON_MASK_MIDDLE: position -= event.relative / zoom pass
@jasonbuford2437
@jasonbuford2437 7 месяцев назад
It zooms for me but it doesn't drag. Do you have any idea why that is?
@julianbirke
@julianbirke 4 месяца назад
It at least doesn't have any errors but still just doesn't work for me :/
@markshumarov
@markshumarov 2 месяца назад
Works for me in Godot 4.2. Thank you very much!
@pvpworld
@pvpworld 2 месяца назад
How could i detect if my Player scene is touching the edge of the screen and stop the camera drag from being able to go past that point? So it can only move a certain direction until the Player hits the edge of the viewport window?
@annaloginova2870
@annaloginova2870 Месяц назад
@@pvpworld one option would be to set limits for your camera. if you know them in advance, you can set them in the inspector, otherwise through code (limit_left, limit_right, limit_bottom, limit_ top props)
@snarekeeper8053
@snarekeeper8053 9 месяцев назад
(At least for Godot 4) You have to do: position -= event.relative * zoom.Inverse() (or zoom.inverse() i am not sure if it is capitalized in GDScript, I rewrote it to C#)
@user-gx2iq9io2o
@user-gx2iq9io2o 7 месяцев назад
the dragging speed is not adjusted quiet right after you zoom in, what i found was: doing this: position-=event.relative / zoom instead of this: position-=event.relative * zoom works quiet well
@zyhgar25
@zyhgar25 7 месяцев назад
How would this code work with using input map? Specifically the mouse drag? I already have a zoom function don't need the speed mod as I am using a toggle camera speed.
@Mestari222
@Mestari222 Год назад
Thanks a lot :) Seems to work fine.
@DrNabeel20
@DrNabeel20 Год назад
Thank you 🔥
@slowik1986
@slowik1986 2 года назад
Any idea or tips you can give for zomming towards mouse position?
@BramwellWilliams
@BramwellWilliams 2 года назад
Yes! In fact its covered with tweens in text in the guide on the itch page c:
@Korn1holio
@Korn1holio 12 дней назад
The enabling and disabling physics_process to toggle zoom seems a bit overkill
@caillef
@caillef 2 года назад
Hey, amazing video! What kind of software are you using to display the code? Thanks!
@BramwellWilliams
@BramwellWilliams 2 года назад
Thankyou! I actually use Godot 4! I have a rich text effect that writes on the text, and I wrote a plugin that extracts syntax highlights from the editor - then play it all in an animation queue c:
@isatche
@isatche Год назад
How to limit camera movement so it doesn't go indefinetely, or so it go on only one axis? Thank you
@EvilChairSlayer
@EvilChairSlayer Год назад
Did you find the answer?
@chunwei7688
@chunwei7688 Год назад
In case you still need it, in the inspector of the camera2D node, under the Limit dropdown, you can set the limits of left, right, top and bottom. If you scroll down to the Editor dropdown, you can also tick the checkbox to Draw Limits and show the limits on your editor screen. So when you instance your camera later on different scenes, you can set these limit individually as I assume you will have some scenes that are different sized :)
@efrenalonso8531
@efrenalonso8531 Год назад
The video may be outdated or something because it doesent work if you follow the shown code. I managed to make it work by copying the code from the link and adding a Tween as a child of the camera node, in case anyone is stuck as I was.
@garyneill1644
@garyneill1644 Год назад
No this doesn't work. I downloaded the files from your site and imported them but no joy. 1. Instructions say to make a Camera2D but the file you download uses PanningCamera2d?? 2. Missing tween in DL Zoom works, drag doesn't. Could you take a look if you have time please? Your demo was excellent to use and has exactly the functionality I'm after :)
@user-ru1gc8hw6v
@user-ru1gc8hw6v 4 месяца назад
I think the zoom in and zoom out is reverted, zoom in is to be more close
@ValaAssistant
@ValaAssistant Год назад
even when multiplying by zoom, it doesnt seem to take into account zoom when moving the camera with the mouse, going very fast zoomed in, and very slow zoomed out Using godot 4
@ValaAssistant
@ValaAssistant Год назад
heck even removing zoom from the position calculation is preferable when zoomed in cause your not outa control
@ValaAssistant
@ValaAssistant Год назад
Fixed it by doing 1 / zoom instead
@baus96
@baus96 11 месяцев назад
thanks a lot :)
@myster1ous23
@myster1ous23 Год назад
Tysm
@unthoughtx5919
@unthoughtx5919 Год назад
how can i set a certain limit to where it cant go
@EvilChairSlayer
@EvilChairSlayer Год назад
Did you find the answer?`
@unthoughtx5919
@unthoughtx5919 Год назад
@@EvilChairSlayer Yup
@EvilChairSlayer
@EvilChairSlayer Год назад
@@unthoughtx5919 Mind sharing?
@unthoughtx5919
@unthoughtx5919 Год назад
@@EvilChairSlayer Yes actually i totally scraped the code he gave to make my own
@EvilChairSlayer
@EvilChairSlayer Год назад
@@unthoughtx5919 o.O k. So you don't want to share ? Or is it long code for camera limits.? You use clamp?
@aaronwolters2422
@aaronwolters2422 11 месяцев назад
You need the property Current selected to work
@demosthenes7902
@demosthenes7902 2 года назад
Godot is strange, I did this myself & it would not work, I downloaded your copy from itch and just copied it for batum & it would not work, I used your project files and it works... Are you doing something special that's somehow hidden somewhere ?
@BramwellWilliams
@BramwellWilliams 2 года назад
I'm sorry to hear that 😥- I specifically avoided doing anything in project settings for this example (in the guide on itch i talked about the alternative of using an input map for handling inputs) All I can really recommend is using lots of print statements to narrow down where things are going wrong
@carlosponcedeleon3903
@carlosponcedeleon3903 2 года назад
i had the same issue until i fixed it enabling CURRENT on the camera 2d node inspector. im not sure if this will help
@slaps_zrz
@slaps_zrz 2 года назад
@@carlosponcedeleon3903 made the same dumb mistake lmao :)
@marcob0
@marcob0 Год назад
how have you downloaded the file?
@markshumarov
@markshumarov 2 месяца назад
Zoom to cursor for Camera2D script in Godot 4.2 : extends Camera2D var zoom_min = Vector2(1, 1) var zoom_max = Vector2(2, 2) var zoom_speed = Vector2(0.1, 0.1) var screen_width = 1920 var screen_height = 1080 func _input(event): if event is InputEventMouseButton: if event.is_pressed(): if event.button_index == MOUSE_BUTTON_WHEEL_UP: zoom_in() if event.button_index == MOUSE_BUTTON_WHEEL_DOWN: zoom_out() func zoom_in(): if zoom < zoom_max: var old_zoom = zoom var new_zoom = old_zoom + zoom_speed update_zoom(old_zoom, new_zoom) func zoom_out(): if zoom > zoom_min: var old_zoom = zoom var new_zoom = old_zoom - zoom_speed update_zoom(old_zoom, new_zoom) func update_zoom(old_zoom: Vector2, new_zoom: Vector2): var mouse_x = get_viewport().get_mouse_position().x var mouse_y = get_viewport().get_mouse_position().y var pixels_difference_x = (screen_width / old_zoom.x) - (screen_width / new_zoom.y) var pixels_difference_y = (screen_height / old_zoom.y) - (screen_height / new_zoom.y) var side_ratio_x = (mouse_x - (screen_width / 2)) / screen_width var side_ratio_y = (mouse_y - (screen_height / 2)) / screen_height position.x += pixels_difference_x * side_ratio_x position.y += pixels_difference_y * side_ratio_y zoom = new_zoom if position != get_screen_center_position(): position = get_screen_center_position()
@thygrrr
@thygrrr 4 месяца назад
Instead of increments in zoom, use proportional factors, e.g. zoom*=0.9 for zoom out and zoom*1.11 for zoom in. Thank me later.
@zsolezk
@zsolezk 5 месяцев назад
The draggin is way off. Nothing like what you have on the end of the video. I put a band aid on it by position -= event.relative / zoom * 2.7 ... works on my system but I guess this is not a solution either.
Далее
I Made the Same Game in 8 Engines
12:34
Просмотров 3,9 млн
How to Make a Good 2D Camera
11:38
Просмотров 392 тыс.
This or That 🛍️
00:52
Просмотров 6 млн
I Melted Wood With Friction
8:44
Просмотров 745 тыс.
Why Making Multiplayer Games SUCKS
6:46
Просмотров 412 тыс.
Smooth Drag N Drop: Godot Guide
9:28
Просмотров 44 тыс.
How Games Make VFX (Demonstrated in Godot 4)
5:46
Просмотров 326 тыс.
This Godot 4 Scene Manager Does it ALL
28:50
Просмотров 22 тыс.
Simulating the Evolution of Rock, Paper, Scissors
15:00