0
votes

I am trying to have my player ignore the collision with an edge collider on a platform i have.

Here's the script that I have added to the player

public class TestMovement : MonoBehaviour
{
public Rigidbody2D ball;
private GameObject purplePlat1;
private GameObject player;

// Start is called before the first frame update
void Start()
{
    purplePlat1 = GameObject.Find("purple_plat");
    player = GameObject.Find("circle-png-44659");

    ball = GetComponent<Rigidbody2D>();
    ball.AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
    Debug.Log("start");
}

// Update is called once per frame
void Update()
{

}

void OnCollisionEnter2D(Collision2D collision)
{

    Physics2D.IgnoreCollision(purplePlat1.GetComponent<EdgeCollider2D> 
  (), GetComponent<CircleCollider2D>());
    Debug.Log("collision");

}
}

The ball is still hitting the platform. I have confirmed that the oncollisionenter method is firing.

enter image description here

enter image description here

2
You should just put the IgnoreCollision in Start(). It remains in effect until you set it to false. - ryeMoss
on collision is called on the collision, in other words, too late to be ignored. its like telling people who come to your house not to come to your house. they are already there. you need to tell them before they come. - Tomer Shahar

2 Answers

0
votes

You can use the layer system of Unity to avoid collisions between both. Set a layer for a player and another for the edge and untick the collision between them.

0
votes

What you can do is create a layer mask for the different type of game objects. Then, open your Physics2D settings.

Physics2D settings

On the bottom part, you can see a matrix of physics objects that can collide to one another. Just uncheck which layer should not collide with the other.

Matrix