Search Unity

Help with ball bouncing around and getting stuck.

Discussion in 'Scripting' started by kikendo, Jul 20, 2017.

  1. kikendo

    kikendo

    Joined:
    Jul 19, 2017
    Posts:
    61
    I hope you guys can help me out because I am not really sure how to fix this.
    I saw similar questions here but all for 2D space, not 3D as I have.
    People recommended messing with the "bounce threshold setting", but I still have issues.

    I have a ball that is bouncing around programatically inside a 3D environment.
    The ball (with a rigidbody) is inside a cube (with a mesh collider). I have a little thingy (Mesh) controlled by me that it can bounce on to make it go different angles.

    I initiate the ball adding a force to it in an axis, and I have a script that, on collision, reflects its velocity.
    The ball has a physic material with the following properties:
    Dynamic friction = 0
    Static friction = 0
    Bounciness = 1
    Friction combine = minimum
    Bounce combine = maximum

    It will just bounce around just fine, but many times, when the ball hits a corner, since they are 90 degrees corners, I feel like the normals cancel each other and the ball either loses bounce in a direction or gets completely stuck if it had hit a 3 wall corner.

    My code on the ball:

    Code (CSharp):
    1. public class BallMovement : MonoBehaviour {
    2.  
    3.     public float thrust;
    4.     Rigidbody rb;
    5.     private Vector3 tempVelocity;
    6.  
    7.     void Start() {
    8.         rb = this.GetComponent<Rigidbody>();
    9.         rb.AddForce(0, 0, thrust, ForceMode.VelocityChange);
    10.     }
    11.  
    12.    void OnCollisionEnter(Collision other) {
    13.         tempVelocity = rb.velocity;
    14.         tempVelocity = Vector3.Reflect(other.relativeVelocity*-1.01f, other.contacts[0].normal); //here I invert the direction of the ball
    15.  
    16.         rb.velocity = tempVelocity; // assign the ball's velocity the new value
    17.      
    18.     }
    19. }
    Thing I tried: checking for the velocity going below a certain value and if it did jump it up to an arbitrary minimum value. It didn't work, ball still gets stuck to edges.

    Some other data that might point to misconfiguration: I did the Reflect Vector3 method because just using the material I made and uncheck gravity influence on the object wasn't bouncing it in a perfectly elastic form, it would eventually stop as if there was "friction" in the air.

    Something I read on another thread: would making the object Kinematic help? With the code I currently have, switching that option on does not move the ball at all at the beginning. Makes sense, because I am using force to move it around.

    There's something else that would be nice to have and could be related to fixing this. Sometimes the angle the ball bounces at is too sharp and makes for annoying movement. I want to remove realism from this, so I think what I need to do, is make sure the normal of the object it is colliding with is within a specified "range" so it doesn't do movements I do not want. Unfortunately for me, I have forgotten all my angle/vector math from school and I have no idea how to go at this :p

    Would that solution also fix the weird behavior of the ball getting stuck to the corners?

    Any help appreciated.

    [EDIT] Noticed what was wrong with normal physics not working, it was that Bounce Threshold factor. Reducing it made the ball bounce even if I disable my Reflect code. But the ball still does stuff like roll on a wall. So I think I have to limit the normals coordinates as I thought before, so I can always make it bounce in a desirable angle, even if it isn't realistic!
     
    Last edited: Jul 20, 2017
  2. jcaldw11

    jcaldw11

    Joined:
    Jun 4, 2017
    Posts:
    2
    Are you trying to make your own physics on an object that has a rigidbody attached to it?

    Why wouldn't you just use Unity's system?

    If you have a rigidbody on this ball, I suggest removing it or yes, making it kinematic but then you have to model gravity and such.

    Your system and unity's system are probably interfering with each other.
     
  3. kikendo

    kikendo

    Joined:
    Jul 19, 2017
    Posts:
    61
    Yeah, you might be right that there's some interference.
    For learning's sake, I would prefer to use some of my own "system", as it is simple (no gravity, just bouncing around).
    Also because I want to do things that do not feel "realistic". If I wanted a 100% realistic setup, using Unity's system alone would definitely be the best solution, but I am trying to mess with it slightly to attain non-realistic results.

    I really need to have a way to limit how that ball bounces, so the angle of reflection is only within a certain range. The standard physics system wouldn't let me do that.
    Also I am adding a little bit of velocity every time the ball bounces, so its speed increases with time. This is handled by my reflection script. Again, the standard system wouldn't do that, at best, it reflects the ball with equal force.

    After messing with the physics settings, it seems that my ball doesn't get stuck anymore, but I need to test for longer.
     
  4. jcaldw11

    jcaldw11

    Joined:
    Jun 4, 2017
    Posts:
    2
    Outside of what I remember from graphics... (reflection ..etc) my best guess would be find the normal direction then +/- so many degrees from that?

    I really think you should remove the rigidbody. calculate your own velocity and angle and add your own forces. Don't rely on unity's system at all.

    You can increase the velocity of the ball on a bounce. You just use a physics material with 1 for bounce and then use one of the drop down boxes settings.. multiply or add I think are two of the choices. Make sure the material you're hitting has a physics material with bounce set high as well. You can drop a ball on a surface with those settings and it'll act like flubber haha.. bouncing higher and higher each time. I did it just last night when I was playing with shadows.

    Anyway, the original question was about the ball getting stuck. My guess is still the rigidbody

    I suggest 100 balls at once ;) makes the testing go faster. (mostly kidding)
     
  5. kikendo

    kikendo

    Joined:
    Jul 19, 2017
    Posts:
    61
    Actually, 100 balls sounds like a good test :D

    You say I should remove the rigidbody, but then recommend to mess with a physics material. Wouldn't that require a rigidbody?

    Thanks so much for your help so far.