Search Unity

Check for collision with ignored layer

Discussion in 'Scripting' started by TheGrovesy, Oct 27, 2020.

  1. TheGrovesy

    TheGrovesy

    Joined:
    Jan 8, 2013
    Posts:
    25
    I want to prevent collisions with platforms when the player is moving upwards, and enable them when they are moving back down. I have achieved this by putting the platforms on a seperate layer and enabling/disabling IgnoreLayerCollision between the player and platform layers depending on direction. This in general is working, however if the player changes direction when half way through the platform the player will suddenly ping off in a direction or gets stuck.
    Is it possible to detect for this collision before enabling the collision layer? or is there another way to achieve this?
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,909
    Depending on the shape of your character's collider you can use the various Physics.OverlapXXX methods. There are also Physics2D versions if this is a 2D game.
     
  3. TheGrovesy

    TheGrovesy

    Joined:
    Jan 8, 2013
    Posts:
    25
    Many thanks that worked for me. My player character uses a capsule collider, its annoying you can't just pass that in, but I found some code to convert the collider to the points that method needs.