0 votes

Hi. I have in my top down game walls TileMap. And all of the walls tiles has a collision shape. And i have a area2d node, that moving with [position += n] method. And they can be destroyed when touch the wall. and i need to bounce they from the walls. I know about bounce or reflect Vector2 methods, but i can't advance they's for that methods. Thanks for help!

(sorry for bad eng)

Godot version 3.5
in Engine by (18 points)

1 Answer

0 votes

Hey there!

If you are using some kind of speed variable in your projectile, all you got to do when you want to reflect it is invert its speed, horizontally if you hit a vertical wall and vertically if you hit a horizontal wall.

You can determinate if the projectile hits the wall vertically or horizontally in many ways, one of the easies is basically check the difference between the position of the colliding and collider instances:

if body.position.x < position.x:
    # Body is hitting the Area2D on the left

By combining those verifications you can determinate if you are going to invert the X axis speed or the Y axis speed.

by (20 points)
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.