1
\$\begingroup\$

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.

\$\endgroup\$
4
  • \$\begingroup\$ Looks like you're making the common error of moving the transform instead of the rigidbody when you want physics behaviour. See this answer for more details on why that causes problems. \$\endgroup\$
    – DMGregory
    Oct 5, 2018 at 11:45
  • \$\begingroup\$ Can you post a screen of your scene? \$\endgroup\$
    – clankill3r
    Oct 6, 2018 at 13:58
  • \$\begingroup\$ It also looks like your circle has a very small radius, which could cause precision errors. What happens if you significantly increase the radius? \$\endgroup\$
    – Ed Marty
    Oct 6, 2018 at 14:45
  • \$\begingroup\$ @EdMarty I think I already tried that, and it didn't really help. The ball would still sometimes just pass through the wall. \$\endgroup\$
    – noClue
    Oct 8, 2018 at 8:54

1 Answer 1

1
+50
\$\begingroup\$

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.

\$\endgroup\$
16
  • \$\begingroup\$ 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". \$\endgroup\$ Oct 5, 2018 at 17:28
  • \$\begingroup\$ @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. \$\endgroup\$
    – Engineer
    Oct 7, 2018 at 6:58
  • \$\begingroup\$ 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. \$\endgroup\$
    – Alakanu
    Oct 7, 2018 at 17:06
  • \$\begingroup\$ @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. \$\endgroup\$
    – noClue
    Oct 8, 2018 at 8:26
  • \$\begingroup\$ 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. \$\endgroup\$ Oct 8, 2018 at 17:04

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .