Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Hazelnuttyz · Jun 22, 2021 at 08:38 AM · physics2dcollider2denemy aideathcombat

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
1

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hazelnuttyz · Jun 22, 2021 at 03:06 PM 0
Share

okay thanks. but what do i add to the code that i already have here @Mrpxl ?

avatar image
0

Answer by Ammut88 · Jun 22, 2021 at 07:06 PM

Can you turn off the collider and the rigidBody? or set it to kinematic?

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hazelnuttyz · Jun 22, 2021 at 08:05 PM 0
Share

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

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

130 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges