Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

GameObject won't "bounce" off wall

Discussion in 'Getting Started' started by whatupo9, Jun 27, 2023.

  1. whatupo9

    whatupo9

    Joined:
    Jan 23, 2022
    Posts:
    2
    I am making a pong game, and I want the ball to bounce when it hits the edge of the screen. whenever the ball should bounce, it just goes straight past the edge and off the screen, and I don't get anything in the console.

    I have a rigid body and a box collider on the ball, and a box collider on each edge that i want it to bounce. Here is the code, the x value of the vector 3 should just stay the same

    Code (CSharp):
    1.  
    2. Vector3 direction;
    3.  
    4. void OnCollisionEnter2D()
    5.     {
    6.         direction.y = -direction.y;
    7.         Debug.Log("Bounce");
    8.     }
     
  2. whatupo9

    whatupo9

    Joined:
    Jan 23, 2022
    Posts:
    2
    Solved it myself
     
  3. RichAllen2023

    RichAllen2023

    Joined:
    Jul 19, 2016
    Posts:
    1,026
    What did you do to solve it? Folk would like to know :D