Search Unity

Projectile Bounce Problem

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

  1. tladdrout

    tladdrout

    Joined:
    Apr 7, 2009
    Posts:
    39
    Hello,

    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,

    -Taylor

    PS. Here is some code:
    Code (csharp):
    1.  
    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
    16.  
    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

    GargerathSunman

    Joined:
    May 1, 2008
    Posts:
    1,572
    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

    tladdrout

    Joined:
    Apr 7, 2009
    Posts:
    39
    Thanks,

    I will give that a try.

    Edit:

    That worked. Thank you Gargerath.

    -Taylor
     
  4. MikeTyke

    MikeTyke

    Joined:
    Dec 16, 2015
    Posts:
    1
    Hi

    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