Search Unity

  1. Get the latest news, tutorials and offers directly to your inbox with our newsletters. Sign up now.
    Dismiss Notice

Issue with a ball on clone game

Discussion in '2D' started by Niko_Passos, Apr 20, 2021.

  1. Niko_Passos

    Niko_Passos

    Joined:
    Jul 15, 2020
    Posts:
    3
    I have an annoying issue on the ball in the breakout clone.
    Every time the ball hit a wall with the paddle at the same time, or, the paddle hit the ball in the opposite direction, let's say:

    The ball going down from the left to right and I rush the paddle from the right to the left to avoid the ball from touching the ground, the moment the ball hits the paddle, it bounces, but it bounces slower, sometimes far slower, almost stopping.

    I even tried to check the actual speed of the ball, with a script to show the current speed and set for the slowdown to happen again. But in the system, the ball is considered to be of the same speed as the initial set speed, it says 600, but still moves far slower than the set speed of 600.
     
  2. ed_s

    ed_s

    Unity Technologies

    Joined:
    Apr 17, 2015
    Posts:
    152
    For this kind of problem, you can improve your chances of getting help by adding a code sample.
     
  3. Niko_Passos

    Niko_Passos

    Joined:
    Jul 15, 2020
    Posts:
    3
    Ok, posting the code:
    This one for the brick

    Code (CSharp):
    1. public class Brick : MonoBehaviour
    2. {
    3.     public Ball ball;
    4.  
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.  
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.      
    15.     }
    16.     private void OnCollisionEnter2D(Collision2D col)
    17.     {
    18.         if (col.gameObject.CompareTag("Breaker"))
    19.         {
    20.             ball.normalSpeed = -ball.normalSpeed;
    21.             Destroy(gameObject);
    22.         }
    23.     }
    24. }
    This is for the ball

    Code (CSharp):
    1. public class Ball : MonoBehaviour
    2. {
    3.     public Rigidbody2D rb2D;
    4.     //public Transform paddle;
    5.     public float normalSpeed;
    6.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         rb2D = GetComponent<Rigidbody2D>();
    11.         rb2D.AddForce(Vector2.up * normalSpeed);
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update()
    16.     {
    17.      
    18.     }
    19.  
    20. }
    And this is from the padddle

    Code (CSharp):
    1. public class Paddle : MonoBehaviour
    2. {
    3.     public float speed = 1.5f, horizontalLimit = 17.5f;
    4.  
    5.     private  Rigidbody2D paddle;
    6.  
    7.     // Start is called before the first frame update
    8.     void Start()
    9.     {
    10.         paddle = GetComponent<Rigidbody2D>();
    11.        
    12.     }
    13.     // Update is called once per frame
    14.     void Update()
    15.     {
    16.         Bounds();
    17.        
    18.     }    
    19.    
    20.     public void Bounds()
    21.     {
    22.         paddle.velocity = new Vector2(Input.GetAxis("Horizontal") * speed, 0);
    23.         // Keep player within the bounds
    24.         if (paddle.transform.position.x + 3 > horizontalLimit)
    25.         {
    26.             transform.position = new Vector3(paddle.transform.position.x, transform.position.y, transform.position.z);
    27.             paddle.velocity = Vector2.left;
    28.         }
    29.         if (paddle.transform.position.x - 3 < -horizontalLimit)
    30.         {
    31.             transform.position = new Vector3(paddle.transform.position.x, transform.position.y, transform.position.z);
    32.             paddle.velocity = Vector2.right;
    33.         }
    34.     }
    35. }
     
unityunity