Ball glitching through paddle and changing speed

Hello everyone.

Really enjoying the course so far! However, I seem to have hit a snag.

I tried to challenge myself a bit, and changed the shape of the paddle, and adjusted the Collider appropirately.

However, when testing, occassionally, the ball would glitch into the paddle, and change speeds. I can also see the ball sprite going into the paddle a bit when I play normally.
Here is a video of the issue: https://drive.google.com/file/d/1nh14ZK4v3l1woZbqzcxX80KvxlVWC1GV/view?usp=sharing

I have also implemented a randomized direction for the ball on start.
Its an option I can toggle, and if toggled, it will randomize the X attribute of the starting vector, and then adjust the Y attribute appropriatelly (So that speed isn’t changed between getting a straight up shot vs the most diagonal one)

//config params
    [SerializeField] bool randomizeXLaunch = false;
    [SerializeField] float xLaunch = 1f;
    [SerializeField] float yLaunch = 8f;
    float ballSpeed;

    //state
    private Vector2 paddleToBallOffset;
    private bool gameStarted = false;

    //cached refference
    Paddle paddle;

    // Start is called before the first frame update
    void Start()
    {
        paddle = FindObjectOfType<Paddle>();
        paddleToBallOffset = transform.position - paddle.transform.position;
        ballSpeed = Mathf.Sqrt(xLaunch * xLaunch + yLaunch * yLaunch);
    }
.
.
. 
private void LaunchOnMouseClick()
    {
        if (Input.GetMouseButtonDown(0))
        {
            gameStarted = true;
            if (randomizeXLaunch)
            {
                xLaunch = Random.Range(-2f, 2f);
                //Randomizing x vector without adjusting y vector could vary the ball speed by 0.5%
                yLaunch = Mathf.Sqrt(ballSpeed * ballSpeed - xLaunch * xLaunch);
            }
            GetComponent<Rigidbody2D>().velocity = new Vector2(xLaunch, yLaunch);
        }
    }

My question is, is this some issue with colliders? Maybe my speed is a bit too high? Or did I mess with the speed in a wrong way?
(I have followed the course up to creating multiple levels.)

Try setting the ball’s Rigidbody2D Collision detection to Continuos, that should fix the issue, with a setting of Discrete, if the ball reaches a certain speed it could go through objects.

Here’s the full documentation on Rigidbody2D in case you want to know more.

Yup, that fixed the issue, thank you so much!
(For anyone reading this in the future, make follow the courses precisely. Collision detection was in the earlier part of the course, but I did not change it then)

1 Like

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

Privacy & Terms