How to make a ball change its direction when colliding?

In my example, your racket must be called “Player” for the “if” query to work. You don’t have any walls, so you can leave out the “elif” query.

And yes, since you have your racket down you have to swap all vectors, including the important position.y - position.y.

“var dif” ; is the query you are looking for, but also the; “var new_movement”; in order to be able to influence the direction more strongly.

I think that should work, otherwise I don’t know what to do next.

And thanks for the feedback about translators. :laughing:

I usually call it paddle, but it’s fine.

I get what your saying, but I’m not sure how to implement in my code, So could you (if possible) break down the different factors of the code I’m looking for and post them here?

And if you need to, I can post the code I’m using for the ball :smiley:

I have a question:
You haven’t implemented any walls in the game right?
Because that’s the problem in the end, it won’t work.
In your case, in order for the ball to fall down again, you need
a RigidBody2D. So you have to use gravity so that the ball can fly or fall downwards. And then I think you need a bounce so that the ball flies up again.
Unfortunately I can’t help yet,
I just tried to recreate your game to understand why the code doesn’t work. My code only works because there are walls in my game. I’ll just send you the link to my project, you’re welcome to import it into Godot and take a closer look at everything I’ve done, but as I said, if you don’t want to install walls, my code is useless.

https://drive.google.com/drive/folders/1Ia4BHqTdvY8TArdO0_NYJHBrtSr8PNLT?usp=drive_link

Unfortunately, I can’t break down exactly why something works or doesn’t work with my code because I’m still a beginner and haven’t understood everything yet.

I understand and it’s ok, I’ll just try and see where I can implement your code in my game, Thanks anyways :slight_smile:

Also yes, I have walls implemented in my game

IT WORKS, The ball can now change it’s direction when hitting a paddle and can sense walls too.
I’ll try and see how to understand the code, and tweak it to what I need, but for now, I think I have my solution, Thank you very much :smiley:

1 Like

Very cool, I’m glad it worked after all. :smile:

I might come back again and make another post if there is an error that I’m experiencing with what I’m making, but other than that, Thanks :smiley:

Also how would you fix the bug where if you touch the ball in the middle of the paddle, it moves slowly, but when you touch the ball at the edge of the paddle, it goes really fast?

I might actually try to fix the issue myself if there won’t be any relpy, but other than that, Thanks for helping me out :smiley:

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.