Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

One-sided collision?

Discussion in 'Editor & General Support' started by Marscaleb, Jun 20, 2014.

  1. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    973
    Is there any way to have an object's collision only respond to one direction or one side?
    As the most prominent example, have it only block on the top, so if someone lands on the object it will stop their decent, but if they were coming from underneath it they could just jump right through it
     
  2. drewradley

    drewradley

    Joined:
    Sep 22, 2010
    Posts:
    3,063
    I think quads might have one sided collisions. I know that backfacing polygons don't register collisions so you might have to import a simple one-sided mesh. You can also probably use two colliders, one as a trigger on the bottom that turns the other one off.
     
  3. Marscaleb

    Marscaleb

    Joined:
    Jan 7, 2014
    Posts:
    973
    That would only work if there will only ever be one object at a time that needs to collide with the surface. Otherwise, an object standing on the surface would fall through when something else comes up underneath.

    As to your first idea, it seems that any 3D physics object will not interact with a 2D physics object. (Unless there is something I am doing wrong, but I tried a lot of different 3D physics collisions and none of them worked.) And since I'm working with 2D I can't test that idea.
     
  4. AlanGameDev

    AlanGameDev

    Joined:
    Jun 30, 2012
    Posts:
    437
    Hello there.

    If it's only for the player, you can just ignore the collision for the layer of the player, here is a old snippet I used for that purpose (2D, works good with edge collider, you have to tune the linecast coords and layers):

    Code (csharp):
    1.  
    2.     if (Physics2D.Linecast(transform.position+Vector2(0.5,1.5), transform.position+Vector2(0.5,-0.5), 1<<12).collider == null)
    3.         Physics2D.IgnoreLayerCollision (9, 12, rigidbody2D.velocity.y > 0);
    4.     else
    5.         Physics2D.IgnoreLayerCollision (9, 12, true);
    6.  
    For multiple objects, the easiest solution (imho) is to set them kinematic when they're ascending and manually update their position/velocity until it stops colliding. Note that you can apply 'forces' to kinematic bodies and they'll work just as well but won't suffer the effects as the rigidbodies, but that's trivial to do manually (i.e. decelerate).

    I like to cast rays but you may want to store the last velocity so if it collides to the platform you just re-apply that velocity and decelerates it manually until it stops colliding with the platform.

    EDIT:
    Just a minor heads-up: The kinematic rigidbodies will 'discard' linear damping, but that's hardly any important, in any case you can apply it manually, otherwise it's just a matter of decelerating it according to the gravity.