How To Bounce Objects Hitting The Camera Edge?

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


I’m trying to make a projectile that bounces off the screen edge. Is there any other way to do this without placing StaticBody2D nodes at the outer edges of the viewport/camera?

I attempted to use a Rect2 covering the whole viewport/camera. I used conditionals to check if the object hit the edge of it. This method works so far, but I don’t know how to accurately mimic the behavior of KinematicBody2D’s move_and_collide()'s bounce() method when the projectile hits an edge.

Code snippet for projectile

Thanks in advance.

:bust_in_silhouette: Reply From: Gluon

If the camera is static you could add a section in the script for the projectiles which says for example

if position.x < 0 then playermoveX = 1 

in other words set limits to how far up, down and left/right the sprite can go and then change its movement states if it reaches those values.

Not sure why you are trying to avoid static bodies at the end of the screen though I would suggest that is the best method to be honest.

I should’ve mentioned it in the title. However, in my case, the camera isn’t static as it follows the player since the camera is a child of the player, which is why I’m hesitant to use static bodies as children. Decoupling the player from the camera by making the camera its individual node would allow me to add static bodies as its children with no problem. I’m just unsure what the performance impact will be if I script the camera to follow the player consistently.

The example you gave would only let me change the direction the object moves on an axis, but I’m unsure how to make it feel like it’s bouncing around like the ball in Pong.

OdTogerus | 2022-04-09 17:30