How to tell when object moves out of camera boundary/view

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Skydome

I have two players. Camera is attached to player 1. When player 2 goes out of camera view I want to print(“out of bounds”). It’s a 2D game with camera2d node.

How should I go about doing that?

Have you tried checking the root Viewport’s boundaries against the second player’s position? For example:

# Do this check when the second player moves.
if player2.position > get_tree().get_root().size:
    print("Player 2 is out of bounds.")

I don’t know whether this will work, but it’s a start.

Ertain | 2022-06-24 23:39

Thank you! But what if camera is zoomed out. I guess I will have to do some adjustments.

Skydome | 2022-06-25 09:54

:bust_in_silhouette: Reply From: BigTuna675

Hi, you might find this tutorial from Kids Can Code helpful: Entering/exiting the screen.

Specifically the part about VisibilityNotifier2D.

Good luck!

Thank you! I will try this.

Skydome | 2022-06-25 09:54

The page says: 404 not found

AgSmith95 | 2023-01-19 18:36

It seems like the Kids Can Code GitHub page got moved, this looks like the same page here:
https://github.com/kidscancode/godot_recipes/blob/master/src-3/content/2D/enter_exit_screen.md

cmayk | 2023-03-04 22:00