I’m making a top down dungeon crawler game and I want the enemies to bounce off the wall when they hit it. However, I don’t want this to happen to the player. I also don’t want the enemies to bounce off other objects, like the player. What can I do to make this work?
If you want different bounciness depending on the hit object, you can’t use a simple OhysicMaterial anymore, but have to use a script.
Just implement OnCollisionEnter2D:
private Rigidbody2D rb2D;
private void Awake()
{
rb2D = GetComponent<Rigidbody2D>();
}
private void OnCollisionEnter2D(Collision2D collision)
{
var bounciness = 0f;
if(collision.collider.CompareTag("Player")) // replace check with whatever
{
bounciness = 1;
}
rb2D.velocity += collision.relativeVelocity * bounciness;
}