Search Unity

How to keep a ball bounce with a constant speed?

Discussion in 'Physics' started by elikozinets99, Apr 8, 2017.

  1. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    I need a way to keep a ball that bounces on the floor bounce with a constant speed, the bounciness is set to '1' and the friction is set to '0'.
     
  2. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Is there a 'roof' or gravity involved? I'm trying to grasp what you want to see happen and what's involved.
     
  3. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    Yes, there is roof and gravity
     
  4. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay.. so you want it to bounce between the top "roof" and the bottom "Ground/floor" .. bounciness = 1 and no friction sound good, but if the object is affected by gravity (just thinking out loud here), then that would prevent what you're desired behaviour, I would think. Right?
    How does it look if you disable gravity on the ball itself?
     
  5. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    The ball just stands where it is
     
  6. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yep, err.. lol (my bad!) Okay, of course you just have to give it a little gentle touch to get it going lol.
    Set its velocity to a value or addforce to it (because now it won't move at the start because it doesn't fall) :)
     
  7. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    Which value do you recommend for the speed?
     
  8. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Anything just to get it going try whatever you think. You can always change it.
    Any value should give you a sense of whether this is doing what you're hoping for.
     
  9. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    Code (CSharp):
    1. private Vector2 v;
    2.     // Use this for initialization
    3.     void Start () {
    4.        
    5.     }
    6.    
    7.     // Update is called once per frame
    8.     void Update () {
    9.         v = new Vector2(0, 2);
    10.         GetComponent<Rigidbody2D>().AddForce(v);
    11.     }
    The ball keeps accelerating when it hits the walls
     
  10. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    yes, just move that to start (only need to set it once, I think, with addforce in your scenario).
     
  11. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    Didn't help the ball gets faster each time it hits the walls
     
  12. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    okay, just for peace of mind if you set velocity instead of addforce (set like 1 or something just to start) does it still accumulate speed?
     
  13. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    Yep
     
  14. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay.. So I tested some things out :) I'll be right back after i complete a minute more or so. I think i have it working
     
  15. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    Does it work ?
     
  16. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Here's what I got/did: AddForce of 300 (?! lol) gave me +/- 6 (depending on the direction, but it was constant.
    velocity set to 3 gave me +/- 3 (much simpler to follow for me)
    I made 2 materials, one for the wall & one for the ball, but later ended up making their settings the same.
    So, make 1 material .. bounciness = 1, drag all off (make sure ball's rigidbody drag is also off)
    and I set the bounce combine to "maximum" but I doubt that matters now that they all have the same value.
     
    Last edited: Apr 8, 2017
  17. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yep try what I said and let me know what results you get :)
     
  18. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    For completeness' sake, my sphere was set to have a mass of '1'. And if the force/velocity I applied was smaller than (whatever threshold), though the ball would move, upon contact (regardless of bounciness, etc), it would freeze at the wall.. not bounce [back].
     
  19. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    Didn't understand the lines I bolded
     
  20. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Okay. If you right-click down in your assets area, like where scripts are made, etc.. You see a list of options.
    Go to the 'create' menu and look for "Physics Material" & make one of those.
    then you'll see what I mean. Once you've set its settings, select your walls + ball and drag the physics material into their colliders. (it accepts a physics material there).
    :)
     
    elikozinets99 likes this.
  21. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    friction is 0?
     
  22. elikozinets99

    elikozinets99

    Joined:
    Apr 6, 2017
    Posts:
    35
    I did it and it actually worked!!

    thank you man!
     
  23. methos5k

    methos5k

    Joined:
    Aug 3, 2015
    Posts:
    8,712
    Yay :) Awesome. I'm glad.
     
    elikozinets99 likes this.
  24. darkknight131714

    darkknight131714

    Joined:
    Sep 8, 2017
    Posts:
    1
    Thx This helped me too
     
  25. vipulnathani20

    vipulnathani20

    Joined:
    Jul 19, 2018
    Posts:
    3
    Hello...I am facing same problem. If any one is trying to help me or send me just basic project of this then it would be great. Thanks in advance.
     
  26. andfxx27

    andfxx27

    Joined:
    Aug 26, 2019
    Posts:
    1
    Hey sorry if this is counted as bumping old thread, but this actually help me too :), thank you!!