Search Unity

OnCollision and AddForce

Discussion in 'Editor & General Support' started by Farox, Oct 12, 2014.

  1. Farox

    Farox

    Joined:
    Oct 12, 2014
    Posts:
    16
    I started with a simple task: To have a steadily bouncing ball.

    However with bounciness set to 1 that ball keeps bouncing higher and higher.

    So I added this to the OnCollision:

    rigidbody.AddForce(0,Toolbox.Instance.maxBounceHeightIdle - collision.relativeVelocity.y,0,ForceMode.VelocityChange);

    Where Toolbox.Instance.maxBounceHeightIdle is a static value, in this case 5.

    This works as expected and I can now apply input to the ball, moving it along x axis by adding force as necessary and the y bounce always stays in that 5 limit range.

    The problem arises when I collide with something that is vertically. It seems that I am getting the wrong values in collision.relativeVelocity and suddenly the ball bounces very high.

    Any pointers?

    Here is a video of what I mean. It's that huge bounce near the end that bothers me:

    http://gfycat.com/AchingVeneratedHochstettersfrog
     
  2. Farox

    Farox

    Joined:
    Oct 12, 2014
    Posts:
    16
    I suck at math. I think this fixed it:

    if(collision.relativeVelocity.y > 0)
    rigidbody.AddForce(0, Toolbox.Instance.maxBounceHeightIdle - collision.relativeVelocity.y, 0, ForceMode.VelocityChange);