Unity2D - Ignore collision with edge collider

做~自己de王妃 提交于 2019-12-10 12:13:33

问题


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.


回答1:


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.




回答2:


What you can do is create a layer mask for the different type of game objects. Then, open your 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.



来源:https://stackoverflow.com/questions/55387846/unity2d-ignore-collision-with-edge-collider

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!