Ball does not jump

When I click the mouse button, the ball does not bounce, but starts flashing, and then falls from paddle.
In addition, after several times turning on the game mode, the texture of the ball goes behind the texture of the paddle.

Here is my code.

public class Ball : MonoBehaviour 
{

    [SerializeField] Paddle paddle1;
    [SerializeField] float vectorX = 2f;
    [SerializeField] float vectorY = 15f;
    

    Vector2 paddleToBallVector;
    bool hasStarted = false;

    void Start()
    { }
      
    

    // Update is called once per frame
    void Update()
    {
        if (!hasStarted)
        {
            LockBallToPaddle();
            LaunchBallOnMouseClick();
        }
      
    }

    private void LaunchBallOnMouseClick()

    {
        if (Input.GetMouseButtonDown(0))

        {
            hasStarted = true;
            GetComponent<Rigidbody2D>().velocity = new Vector2(vectorX, vectorY);
        }
    }

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

Hi,

Welcome to our community! :slight_smile:

What are the values of vectorX and vectorY in your Inspector?

If the ball is blinking, check the z-position of the background. It must not have the same value as the ball’s. Is the z-position of the background set to 10, the z-position of the camera to -10 and the z-position of the other game objects to 0? If the ball disappears behind the paddle but you want to have it rendered in front of the paddle, adjust the z-position or increase the “Order in Layer” value in the SpriteRenderer component.

Thank you for your answer!
I checked the z-positions and everything matches your recommendations.
When I start the game and press the button, the ball does not jump, but flashes, and when the paddle moves, it just falls down.

Despite this I fixed the problems with disappearing ball, thanks to your recommendations.

I’m glad that one issue is fixed. :slight_smile:

Please check if there is a collider attached to the background.

And create a new method in the Ball class:

void OnCollisionEnter2D (Collision2D col)
{
    Debug.Log("Ball collided with: " + col.gameObject.name);
}

Save the script and run your game. Once the problem occurs, check your console. Maybe you will get a message which helps you narrow down the issue a bit further.

This topic was automatically closed after 22 hours. New replies are no longer allowed.

Privacy & Terms