I have a script that makes the camera follow the player using tweens. It only adjusts the y position when the player lands on a platform to reduce unnecessary adjustments. Its works well and can keep up with the player except if the player falls a long way. In this case, the player may be offscreen before landing. I would like to check in code if the player is, say, in the bottom quarter of the screen, and if so, I will ignore my usual camera code and just go ahead and start following the player y position as he falls, that way the player could never fall offscreen. I don't know how to ask it in code though. For example, if the player position is in the bottom quarter of what the camera currently sees.
Comparing player position to position within the viewport
Asked Answered
Elaterite
You can convert your local coordinates (those of the player in your case) to screen coordinates. So you could check in every physics frame if your player is in the bottom quarter.
var screen_coord = get_viewport().get_screen_transform() * get_global_transform_with_canvas() * local_pos
Or you could compare your player position with your camera position (taking the zoom, so the scale of your camera node into consideration as well).
That helps a lot. Thank you!!
© 2022 - 2024 — McMap. All rights reserved.