Scripts properly written but ball slows down unexpectedly

I don’t get any compilation or syntax errors. The gravity in project settings has x and y both set to 0. My paddle and ball both have bounce. Yet after hitting a few times, the ball just suddenly slows down. After hitting once or twice more, it slows down even further. I’m using Unity 2018.4.23 LTS if this makes any difference. Any idea why this might suddenly happen?

Yes, it’s probably related to a bug in your code. Hard to tell beyond that without seeing your code.

ok i really hope you can help. My code is here, including all headers.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Ball : MonoBehaviour
{
    [SerializeField] Paddle paddle1;

    Vector2 paddleToBallVector;
    bool hasStarted = false;

    // Start is called before the first frame update
    void Start()
    {
        paddleToBallVector = transform.position - paddle1.transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if (hasStarted == false)
        {
            LockBallToPaddle();
            LaunchOMouseClick();
        }
    }

    private void LockBallToPaddle()
    {
        Vector2 paddlePos = new Vector2(paddle1.transform.position.x, paddle1.transform.position.y);
        transform.position = paddlePos + paddleToBallVector;

    }

    private void LaunchOMouseClick()
    {
        if (Input.GetMouseButtonDown(0))
        {
            hasStarted = true;
            GetComponent<Rigidbody2D>().velocity = new Vector2(2f, 15f);

        }
    }
}

If you’re using realistic physics on the ball, then most likely the first thing is to assign the collider a Physics material that has a bounce of 1 and a friction of 0. Your ball is probably just losing energy on each collision.

This tutorial also seems to give some other tips for making a breakout-style game: https://noobtuts.com/unity/2d-arkanoid-game

1 Like

Thank you so much. I think it works now. But it’s strange that with the earlier version, i didn’t have this problem. why was friction changed when unity creates it? Yes I’ve seen that link but my tutorial is step by step with help. When I’m better, I will consider trying that out.