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. Dismiss Notice

ball bouncing C#

Discussion in 'Scripting' started by YoniBE, May 22, 2014.

  1. YoniBE

    YoniBE

    Joined:
    Aug 2, 2013
    Posts:
    4
    Hello! :)
    I'm trying to create a pong game and I have an problem with the ball bouncing..
    The ball is losing energy.
    I have a physical material (2D) that I applied to all the objects but it still not working..
    **The game is 2D (I don't have any 3D models at the game)**

    How can I fix this?
    thx for help! :)
     
  2. Patico

    Patico

    Joined:
    May 21, 2013
    Posts:
    886
    Does they have RigitBody component?
     
  3. YoniBE

    YoniBE

    Joined:
    Aug 2, 2013
    Posts:
    4
    the ball and the paddles yes.
    the walls no.
     
  4. GarthSmith

    GarthSmith

    Joined:
    Apr 26, 2012
    Posts:
    1,240
    I had a similar problem. Instead of trying to get the Physics 100% right (which I didn't want to spend the time on) I attached this to my ball instead.

    Code (csharp):
    1.  
    2. // Adjust this to adjust speed of the ball.
    3. public float ballSpeed = 5;
    4.  
    5. void FixedUpdate() {
    6.   rigidbody.velocity = rigidbody.velocity.normalized * ballSpeed;
    7. }
    8.  
    EDIT: I haven't had a chance to work with the 2D physics yet, so that code might be a bit different.
     
  5. YoniBE

    YoniBE

    Joined:
    Aug 2, 2013
    Posts:
    4
    thanks!! :)
    It is works!

    (For 2D it is very simple to change:
    Code (csharp):
    1.     void FixedUpdate()
    2.     {
    3.         rigidbody2D.velocity = rigidbody2D.velocity.normalized * ballSpeed;
    4.     }