Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice

The perfect bouncing ball

Discussion in 'Scripting' started by Martin-Schultz, Jun 30, 2009.

  1. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    I'm trying to archieve a perfect bouncing ball. For that I set the bouncyness of the phyiscs material of the ball to 1 and the friction to zero as well as the drag/angular drag to zero. The problem is that the ball seems to gain energy each bounce and jumps higher and higher. Any idea why this happens?
     
  2. MikeHergaarden

    MikeHergaarden

    Joined:
    Mar 9, 2008
    Posts:
    1,027
    I've tried to achieve the same, but tweaking physics etc. doesn't seem to be enough..there is "leakage" or some other inaccuracy.

    In the end I used calculations to lerp to the "right" height.
     
    Deleted User likes this.
  3. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Yeah. The pitty is I had it once working and hence I know it works, but I lost that project due to a crahs. I'm trying now to despairingly re-create the effect... :)
     
  4. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    What do you mean by perfect bouncing ball? Do you mean it bounces forever, or that it will bounce then eventually come to rest? It sounds like you are trying to achieve the former. If so, I think it works "out of the box." When I first started fiddling with Unity a few months ago, I created a sphere and gave it a rigidbody. I placed it above a plane and hit play. The thing wouldn't stop bouncing, but never gained altitude either.
     
  5. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    Yes, a forever bouncing ball that does not loose energy. I tried it with several settings meanwhile, changing physics materials and all that, but still it gains height every bounce. I attached a sample project which demoes that effect.
     

    Attached Files:

  6. Maker16

    Maker16

    Joined:
    Mar 4, 2009
    Posts:
    779
    When I get home, I'll do what I did before. If it behaves the same way, I'll post the settings that are on the material/rigidbody.
     
  7. podperson

    podperson

    Joined:
    Jun 6, 2006
    Posts:
    1,371
    Store the ball's velocity in a member variable somewhere. After any collision, take its new velocity, normalize it, and then multiply by the stored velocity. This will eliminate any small errors caused by the physics engine.
     
  8. Martin-Schultz

    Martin-Schultz

    Joined:
    Jan 10, 2006
    Posts:
    1,377
    I found a solution for this! Using the idea Podperson had I left out the normalizing, but only saved on the first bounce contact the velocity. When a ball falls down it usually has a negativ velocity.Y value which gets inverted to positive when the OnCollisionEnter occures. Now I save that value in a member variable and override the balls velocity.y value each time a collision occures. That gives an exact the same height bouncing ball. The cool thing with that solution is, you can "spawn" your ball at any height (as required in my game) and it will always use that spawn height for bouncing then.

    Thanks to all people helping finding a solution!

    That script is attached to the bouncing ball.

    Code (csharp):
    1. var firstTimeVelocitySave = false;
    2. var savedVelocity : Vector3;
    3.  
    4. function OnCollisionEnter(col : Collision) {
    5.     //print("Collision! " + rigidbody.velocity);
    6.     if (! firstTimeVelocitySave) {
    7.         savedVelocity = rigidbody.velocity;
    8.         firstTimeVelocitySave = true;
    9.     }
    10.     myRigidbody.velocity.y = savedVelocity.y;
    11. }
     
  9. floragreste

    floragreste

    Joined:
    Oct 19, 2011
    Posts:
    1
    Hi, I'm a beginner in Unity and I tried to apply your code in 3 cubes I created in my exercise:

    But I can't achieve the effect I want to :eek:

    The 3 cubes just keep going on the y-direction without doing the reverse..

    Can you tell me if I'm doing something wrong?

    Thanks in advance.
     
    Last edited: Oct 19, 2011
  10. powenko

    powenko

    Joined:
    Jan 8, 2012
    Posts:
    1
    Update the code, it could working very good in my case.


    Javascript:


    #pragma strict
    var firstTimeVelocitySave = false;
    var savedVelocity : Vector3;
    var myRigidbody : Rigidbody;
    var BounceRate:float =1.5;

    function OnCollisionEnter(col : Collision) {
    //print("Collision! " + rigidbody.velocity);
    if (! firstTimeVelocitySave) {
    savedVelocity = rigidbody.velocity;
    firstTimeVelocitySave = true;
    }
    myRigidbody.velocity.y = savedVelocity.y;
    savedVelocity.y=savedVelocity.y/BounceRate;
    }



    C#



    using UnityEngine;
    using System.Collections;

    public class ballBounce : MonoBehaviour {




    private bool firstTimeVelocitySave = false;
    private Vector3 savedVelocity;
    //public Rigidbody myRigidbody ;
    public float BounceRate =1.5f;

    void OnCollisionEnter(Collision collision) {
    //print("Collision! " + rigidbody.velocity);
    if (! firstTimeVelocitySave) {
    savedVelocity = rigidbody.velocity;
    firstTimeVelocitySave = true;
    }
    //myRigidbody.velocity.y = savedVelocity.y;
    // myRigidbody GameObject
    gameObject.rigidbody.velocity =new Vector3(0,savedVelocity.y,0);
    savedVelocity.y=savedVelocity.y/BounceRate;
    }

    }






    -------------------------------
    www.powenko.com
    unity tutorials, http://www.powenko.com/en/?page_id=2928
    Powen Ko
     
    Last edited: Jan 8, 2012
  11. clifforj

    clifforj

    Joined:
    Aug 16, 2012
    Posts:
    1
    Alternatively:

    Code (csharp):
    1. #pragma strict
    2. var Bouncibility:float = 1.1;
    3.  
    4. function OnCollisionEnter (collision : Collision)
    5. {
    6.     rigidbody.velocity.y = collision.relativeVelocity.y/Bouncibility;
    7. }
    *values of bouncibility less than 1 will cause an increase of bounce height after each bounce, over 1 causes a decrease in bounce height*

    Attach to something with a RigidBody, fiddle with Bouncibility, watch it bounce.

    I'm not entirely sure of the thought process behind the other solutions, they seem overly complicated to achieve something so simple. Unless I'm missing some other requirement?

    (Sorry to revive old thread, was one of the first things that popped up on Google for this topic though!)
     
    agudmund and chesterhilly like this.
  12. Think10

    Think10

    Joined:
    Nov 1, 2015
    Posts:
    1
  13. An3Apps

    An3Apps

    Joined:
    Nov 22, 2017
    Posts:
    8
    These simple lines of code should work for you:

    C#:

    public Rigidbody rb;

    float storedVelocity;

    void Start() {
    rb = GetComponent<Rigidbody> ();
    rb.velocity = new Vector3(0, 5, 0);
    storedVelocity = rb.velocity.y;

    }

    void OnCollisionEnter(Collision collision)
    {
    rb.velocity = new Vector3 (0, storedVelocity, 0);
    }