Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[Solved] Speed up Ball - Ballbreaker Game

Discussion in '2D' started by zmatiaki, Jul 8, 2019.

  1. zmatiaki

    zmatiaki

    Joined:
    Nov 21, 2018
    Posts:
    13
    Hi,
    I m making a simple ball breaker game and I use the code below to handle ball launch and collisions . Whole procedure uses Vector2 for launch and for angle change as well. My goal is to speed up ball by time. I know I can use coroutines but my issue is how to handle speed when movement is made with vector2 .

    Ball scripts contains bellow code ;

    Launch Ball
    Code (CSharp):
    1.     public void ShootCannonsOnClick()
    2.     {
    3.         if (Input.GetMouseButtonDown(0))
    4.         {
    5.             GetComponent<Rigidbody2D>().velocity = new Vector2(XLanch, YLanch);
    6.             //Myrigidbody2D.velocity = transform.forward * BallSpeed;
    7.             GameStarted = true;
    8.  
    9.  
    10.         }
    11.  
    12.     }
    Change Angle when collide;

    Code (CSharp):
    1.     private void OnCollisionEnter2D(Collision2D collision)
    2.     {
    3.         Vector2 velocityTweek = new Vector2(UnityEngine.Random.Range(0.0f, randomFactor), UnityEngine.Random.Range(0.0f, randomFactor));
    4.         if (GameStarted)
    5.         {
    6.             AudioClip clip = Ballsounds[UnityEngine.Random.Range(0, Ballsounds.Length)];
    7.             MyAudioSource.PlayOneShot(clip);
    8.             Myrigidbody2D.velocity = Myrigidbody2D.velocity + velocityTweek;
    9.         }
    10.  
    11.     }
     
  2. zmatiaki

    zmatiaki

    Joined:
    Nov 21, 2018
    Posts:
    13
    I think I solved after days.Currently works when space is pressed. In case anyone else is wondering ;

    Code (CSharp):
    1.     public void changeSpeed()
    2.     {
    3.         if(Input.GetKeyDown(KeyCode.Space))
    4.         {
    5.             Myrigidbody2D.AddForce(Myrigidbody2D.velocity.normalized * BallSpeed);
    6.         }
    7.  
    8.     }
     
    Last edited: Jul 8, 2019