Search Unity

Which approach to use for collision direction?

Discussion in 'Editor & General Support' started by HannesB, Oct 26, 2013.

  1. HannesB


    Oct 29, 2012

    i am an experienced C# developer and started learning unity some weeks ago. I already created a game in XNA that does the same i want to achive with unity, so this is a "learning game project" for me.

    What i want to do is creating a game similar to this:

    ...i have been playing this a really long time ago. :)

    The "ball" is my player object. It has a rigidbody attached to detect collitions - it's not controlled by physics since this would not work that good in this game.

    I want the ball to behave different, when it hits a brick or the walls, depending which part of the ball (top, bottom, left, right) has hit the brick or the walls. For example: bounce a bit to the left, when a brick was hit with the right side of the ball (as you can see in the video), change the move direction of the ball, when it hits with it's top/bottom.

    So i do not only need to find out if a colission has occured - i also want to find out, which part (top, bottom, left, right) of the ball has collided. I tried, placing invisible blocks at the top, left, right, ... of the ball and use these blocks for colision detection but i don't know if this is a good idea so i wanted to ask unity developers with more experience, how you would solve this?

    Unfortunately i found not tutorial for this - there are many tutorials for collision detection, but non where it matters, which part of the object collided.

    thx for you help,
  2. hippocoder


    Digital Ape Moderator

    Apr 11, 2010
  3. HannesB


    Oct 29, 2012

    i finally got it working the way i want, applaying some minor changes to the code from:

    Code (csharp):
    2. // create particle effect at collition point
    3. ContactPoint contact = collision.contacts[0];
    4. Quaternion rot = Quaternion.FromToRotation(Vector3.up, contact.normal);
    5. Vector3 objectHitPosition = contact.point;
    7. if (collisionParticles != null)
    8.     Instantiate(collisionParticles, objectHitPosition, rot);
    10. //this ray will generate a vector which points from the center of
    11. //the falling object TO object hit. You subtract where you want to go from where you are
    12. Ray MyRay = new Ray(objectHitPosition, objectHitPosition - this.transform.position);
    14. //this will declare a variable which will store information about the object hit
    15. RaycastHit MyRayHit;
    17. //this is the actual raycast               
    18. Physics.Raycast(MyRay, out MyRayHit);
    20. //this will convert that normal from being relative to global axis to relative to an objects local axis
    21. Vector3 MyNormal = MyRayHit.normal;
    23. //this next line will compare the normal hit to the normals of each plane to find the side hit
    24. MyNormal = MyRayHit.transform.TransformDirection(MyNormal);
    26. if(MyNormal == MyRayHit.transform.up)
    27. {
    28.     //you hit the top plane, act accordingly
    29.     print ("Hit TOP plane");
    30. }
    32. //important note the use of the '-' sign this inverts the direction, -up == down. Down doesn't exist as a stored direction, you invert up to get it.
    33. if(MyNormal == -MyRayHit.transform.up)
    34. {
    35.     //you hit the bottom plane act accordingly
    36.     print ("Hit BOTTOM plane");
    37. }
    39. if(MyNormal == MyRayHit.transform.right)
    40. {
    41.     //hit right
    42.     print ("Hit RIGHT plane");
    43. }
    45. //note the '-' sign converting right to left
    46. if(MyNormal == -MyRayHit.transform.right)
    47. {
    48.     //hit left
    49.     print ("Hit LEFT plane");
    50. }