Comparing player position to position within the viewport
Asked Answered
E

2

0

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.

Elaterite answered 20/3 at 8:28 Comment(0)
P
1

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).

Protege answered 20/3 at 8:45 Comment(0)
E
0

That helps a lot. Thank you!!

Elaterite answered 20/3 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.