Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

2d bouncing ball script.

Discussion in 'Scripting' started by kosted, Jun 14, 2015.

  1. kosted

    kosted

    Joined:
    Mar 14, 2015
    Posts:
    104
    Hi everybody, i'm trying to make a 2d bouncing ball. I found in the site of unity a tuto and in some forums some scripts for 3d ball; In these scripts they use some components i don't have in a 3d object. Someone knows how i can do my bouncing ball. I want my ball changes randomly direction when it touches a wall.
    I you have a suggestion, i take.
    thanks in advance
     
  2. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
    To make a 2d ball bounce, put a rigidbody2d and a circle collider 2d on the 2d ball. Then add a 2d physics material on the ball and set its bounciness to 1. When it falls on your 2d ground that has a collider on it, it should bounce.

    To have it bounce in a random direction, look up OnCollisionEnter2D. By using that, you can give the ball a random velocity by using AddForce and some random number with the Random class.
     
  3. kosted

    kosted

    Joined:
    Mar 14, 2015
    Posts:
    104
    Thanks a lot. But what's the name 2d physics material i have to add
     
  4. kosted

    kosted

    Joined:
    Mar 14, 2015
    Posts:
    104
    It's ok. thanks
     
  5. HiddenMonk

    HiddenMonk

    Joined:
    Dec 19, 2014
    Posts:
    987
  6. kosted

    kosted

    Joined:
    Mar 14, 2015
    Posts:
    104
    The link is down : page not found
     
  7. kosted

    kosted

    Joined:
    Mar 14, 2015
    Posts:
    104
    It's work fine and here a tuto to
     
  8. oguzkagan

    oguzkagan

    Joined:
    Feb 23, 2019
    Posts:
    2
    i found the solution in this video:
     
    LordKakabel likes this.
  9. shadyj

    shadyj

    Joined:
    May 21, 2020
    Posts:
    6
    I sorry for asking but how to you make its volicity stay the same when it bounces
     
  10. Oracle_77

    Oracle_77

    Joined:
    Jul 2, 2021
    Posts:
    1
    probably don't still need this but you can set max velocity like this

    public Rigidbody2D rb;
    public float maxSpeed = 10.0f;

    void Update()
    {
    if (rb.velocity.magnitude > maxSpeed)
    {
    rb.velocity = Vector2.ClampMagnitude(rb.velocity, maxSpeed);
    }
    }
     
  11. zedz

    zedz

    Joined:
    Aug 31, 2013
    Posts:
    231
    I believe the equation is

    new velocity = 2 * (V dot N) * N - V

    V = velocity, N = normal of the surface you bounce off
    This will work in 2d or 3d