The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates.

godotengine.org | Twitter

0 votes

Hello.

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.

Godot version v3.4.2.stable.official
in Engine by (21 points)

1 Answer

0 votes

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.

by (3,328 points)

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.

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.