- Home /
how to walk through enemy corpse after it is killed?
so my enemies are using the Dynamic Rigidbody2D because i want them to fall down after they die. However the enemy corpse blocks other enemies from reaching the player as the colliders are still active. if i disable the colliders, the enemy corpse will fall through the ground. How do i make the enemy corpse stay after it dies but not blocking the path of my player/ other enemies.
this is the enemy agro code.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyAgro : MonoBehaviour { public Animator animator;
[SerializeField]
public Transform player;
[SerializeField]
public float agroRange;
[SerializeField]
public float moveSpeed;
Rigidbody2D rb2d;
// Start is called before the first frame update
void Start()
{
rb2d = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
//dist to player
float distToPlayer = Vector2.Distance(transform.position, player.position);
if (distToPlayer < agroRange)
{
//code to chase player
ChasePlayer();
}
else
{
//stop chasing player
StopChasingPlayer();
}
}
void ChasePlayer()
{
if(transform.position.x < player.position.x)
{
//enemy is to the left side of the player
rb2d.velocity = new Vector2(moveSpeed, 0);
transform.localScale = new Vector3(3, 3, 3);
animator.SetBool("IsWalking", true);
}
else
{
//enemy is to the right side of the character
rb2d.velocity = new Vector2(-moveSpeed, 0);
transform.localScale = new Vector3(-3, 3, 3);
animator.SetBool("IsWalking", true);
}
}
void StopChasingPlayer()
{
rb2d.velocity = Vector2.zero;
animator.SetBool("IsWalking", false);
}
}
And here is the Enemy health code that i am using.
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class EnemyHealth : MonoBehaviour { public Animator animator; public int maxHealth = 100; int currentHealth;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
}
public void TakeDamage(int attackDamage)
{
currentHealth -= attackDamage;
//hurt anim
if(currentHealth <= 1)
{
Die();
}
}
void Die()
{
Debug.Log("Enemy died");
animator.SetBool("IsDead", true);
GetComponent<EnemyAgro>().enabled = false;
GetComponent<CircleCollider2D>().enabled = false;
this.enabled = false;
}
}
someone please help thanks.
Answer by Mrpxl · Jun 22, 2021 at 02:33 PM
An approach would be to change enemy colliders layer. Make a collision layer that collides with Ground layer only, and when your enemy dies, change all of it's collider to that layer
okay thanks. but what do i add to the code that i already have here @Mrpxl ?
Answer by Ammut88 · Jun 22, 2021 at 07:06 PM
Can you turn off the collider and the rigidBody? or set it to kinematic?
i tried disabling the colliders but the corpse would just fall through the ground. when i set the rigidbody to kinematic..when the enemy died, it (the corpse) would float in one direction off the screen. thanks for the answer tho @Ammut88
Answer by KayIllustrations · Aug 05, 2021 at 10:34 AM
i found that if you add an edge collider2d and off-set it exactly at the bottom of the circle collider of the enemy then that is what will keep it from falling. as long as your code says to disable specifically the circle/capsule collider then the edge collider is unaffected. the player doesnt have to run into it either since its just a single line along the ground. i ran into this problem today so just in case you or anyone else still has this problem, that was the quick fix for me!
Answer by yashverma30115 · Aug 05, 2021 at 11:49 AM
You can use Layer-Based Collisions
make certain layers object pass through only objects will chosen layer.
so, when your player dies. you can assign it a layer through the script.
if you don't know how to set up Layer-Based Collision.
go to edit > project setting > Physics (or Physics2D depends upon your game)
You can set it there
Your answer
Follow this Question
Related Questions
Detecting top most UI element using Collider2D 0 Answers
Performance issues when moving 2D colliders by code. 1 Answer
Velocity doesnt change properly 1 Answer
2D physics object acts weird when pressed into other physics object 0 Answers
After Collision Matrix change in runtime, effects not immediatly visible 1 Answer