1

In my top down perspective game, I have a ball that bounces off of walls. The ball should behave in a way where it will always bounce off in a 90 degree angle. The ball can only move up, down, left and right and the walls are slanted so that they should always (theoretically) bounce a ball up, down, left or right.

On the ball object, I have a small circle collider 2D and a Rigidbody 2D attached, the walls have a single edge collider 2D attached. Approximately one time out of twelve times, the ball will simply pass through the edge collider, so it's not consistent.

How can I make sure that the ball always registers the collision?

Here's my setup:

enter image description here

The script I use to move the ball:

void Update () {
    switch (direction)
    {
        case 'u':
            transform.Translate(0f, ballSpeed * Time.deltaTime, 0f);
            break;
        case 'd':
            transform.Translate(0f, ballSpeed * -1 * Time.deltaTime, 0f);
            break;
        case 'l':
            transform.Translate(ballSpeed * -1 * Time.deltaTime, 0f, 0f);
            break;
        case 'r':
            transform.Translate(ballSpeed * Time.deltaTime, 0f, 0f);
            break;
    }
}

Additional info:
- every wall is it's own object with it's own edge collider.
- the ball always hits the center of the wall because I center the ball object using the center of the wall it touches. Note: I had issues with the ball passing through walls even before I implemented the centering.

1 Answers1

1

This is happening because collisions happen in fixed update and you are moving the ball by its transform in the update. You should instead use Rigidbody.MovePosition, or apply a force to it instead or make it kinematic and move the transform but in Fixed update. Take a look at the rigidbody manual and this.

Alakanu
  • 705
  • 5
  • 18
  • This is true for sure. However, I have a similar situation that I haven't solved in a year now. I've tried both RigidBody and non-RB physics and found that in some cases even slow moving objects don't always collide "as expected". – Jesse Williams Oct 05 '18 at 17:28
  • @JesseWilliams I suggest you supply sample code of that specific scenario, then, as there is otherwise no way for people to guess at the issue you are experiencing. – Engineer Oct 07 '18 at 06:58
  • Guys, remember that this is a comment section of an answer. Not another question thread. Please keep any comment specific to the resolution of the original question. – Alakanu Oct 07 '18 at 17:06
  • @Alakanu Solved my issue, now the ball always registers the collision. However, now it also behaves strangely when it collides with a slanted wall (/) from the right. Instead of bouncing off from the middle, it "slides" down the wall before continuing straight down. Any idea why that might be? I have a script that centers the ball whenever it hits a wall using the center of the wall, but it somehow works for all interactions except for the ball hitting a / wall from the right side. –  Oct 08 '18 at 08:26
  • I wasn't asking a question, I was giving feedback on the response - that there are still some instanced using kinematics or not, where the engine doesn't describe the motion or collision properly. – Jesse Williams Oct 08 '18 at 17:04
  • @noClue hard to say withouth knowing what is supposed to change the direction the ball travels to – Alakanu Oct 09 '18 at 23:37
  • @noClue That sounds like a new problem. If this answer solved your initial problem, please mark it as the answer. THEN ask a new question. =) – Immersive Oct 10 '18 at 04:27
  • @Immersive I would love to, but I don't want to give up rep every time just to get some attention to my questions... I hoped I might get some more help here before I pick an answer, but whatever. –  Oct 11 '18 at 10:06
  • @noClue we are fellow developers helping each other, don't think of this website as a place where you are being provided a service. If you want more help, either make your question broader or ask another question. – Alakanu Oct 11 '18 at 13:31
  • @Alakanu Who said it was a service? –  Oct 15 '18 at 08:46
  • The fact that you feel like you should receive more. – Alakanu Oct 15 '18 at 09:09
  • @Alakanu That's not a fact, that's what you assume. Thanks for leaving out the @ btw, very sneaky. –  Oct 26 '18 at 07:39
  • ???? "I don't want to give up rep every time just to get some attention to my questions" this is not a fact I assume. This is you thinking you are "paying" us with rep. It's not how it works. – Alakanu Oct 26 '18 at 09:23
  • @Alakanu Again you're just assuming stuff, and why are you leaving out the mentions, do you just want to have the last word or what? I'll let you have it if you want it this badly. Don't put words in people's mouths as if you know what their intention was. Me saying I don't like giving up rep is me commenting on how Stackoverflow doesn't show certain questions enough, so as a result I have to give up rep to make the website show my question more often. Nothing about me expecting service here, more me fighting against the algorithm with a limited amount of rep. –  Oct 26 '18 at 09:42
  • @noClue sir, you are overreacting. I consider this conversation over. – Alakanu Oct 26 '18 at 19:06
  • @Alakanu "Sir", I'm just giving you some of your own medicine. –  Oct 26 '18 at 19:14