Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice
  2. Ever participated in one our Game Jams? Want pointers on your project? Our Evangelists will be available on Friday to give feedback. Come share your games with us!
    Dismiss Notice

Projectile Bounce Problem

Discussion in 'Scripting' started by tladdrout, Apr 15, 2009.

  1. tladdrout


    Apr 7, 2009

    I am working on bouncing projectiles. I know it is easy to have the physics make them bounce and I currently have them bouncing that way.

    The problem is I need to predict where they are going. When I cast rays out and have them reflect off walls the path the projectile follows is different than the path the rays go. I believe that the physics engine must not do pure reflection, maybe because of some sort of mass, drag, or something.

    I am trying to handle bouncing the projectiles myself, but they seem to just move along the wall and not bounce.

    So if someone can help me, what I need is:
    1: how does the physics get the bounce angles so I can emulate it?
    2: how do I get the projectile to go the direction I want?

    The answer to one of these two questions would be appreciated,


    PS. Here is some code:
    Code (csharp):
    2. function OnCollisionEnter(collision : Collision)
    3. {
    4.      var contact : ContactPoint;
    5.      if(numBounces > 0  (collision.gameObject.layer != 8))
    6.      {
    7.           contact = collision.contacts[0];
    8.           var dot : float = Vector3.Dot(contact.normal, (-transform.forward));
    9.           dot *= 2;
    10.           reflection = contact.normal * dot;
    11.           reflection = reflection + transform.forward;
    12.           rigidbody.velocity = rigidbody.TransformDirection(reflection.normalized * 15.0);
    13.           numBounces -= 1;
    14.      }
    15.      else// the else doesn't matter
    I typed the code, not copy and paste, so small errors are not the problem, its the math or how I am using it.
  2. GargerathSunman


    May 1, 2008
    If you want perfect bounces, you'll need to set drag and angular drag to 0 on the projectile's rigidbody, create a physic material with all friction settings set to 0, bounciness set to 1, friction combine set to minimum, and bounce combine set to maximum, and put that physic material on the projectile.
  3. tladdrout


    Apr 7, 2009

    I will give that a try.


    That worked. Thank you Gargerath.

  4. MikeTyke


    Dec 16, 2015

    I am trying to make a pool table type game. I have set all the settings as per Gargerath above and the snooker cue hits the Cue ball fine and the Cue Ball hits the Pot Balls fine but the pot balls get to the wall and lose interest - they creep along the wall and don't bounce.

    Any suggestions will be appreciated

    Don't worry - i found REFLECT

    Attached Files:

    Last edited: Dec 16, 2015