Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Lineweaver · May 03, 2019 at 07:10 PM · 2dcollisionplayercollidersmelee attack

Player attack doesn't recgonize different colliders (unity 2D)

I'm trying to create an enemy with two box colliders, so that if the player hits the left collider, the enemy gets knocked back to the right, and vice versa. The player script is this:

 public class PlayerAttack : MonoBehaviour {

 private float timeBtwAttack;
 public float startTimeBtwAttack;

 public Transform attackPos;
 public float attackRange;
 public LayerMask whatIsEnemies;
 public int damage;

 void Update ()
 {
     if (timeBtwAttack <= 0)
     {
         if (Input.GetKey(KeyCode.Space))
         {
             Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
             for (int i = 0; i < enemiesToDamage.Length; i++)
             {
                 enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage);
             }

             timeBtwAttack = startTimeBtwAttack;
         }
     }
     else
     {
         timeBtwAttack -= Time.deltaTime;
     }
 }

This is the enemy script:

 public class Enemy : MonoBehaviour {

 public int health;
 public float speed;
 public int knockBack;

 Collider2D c;
 public BoxCollider2D left;
 public BoxCollider2D right;   

 void Update ()
 {
     transform.Translate(Vector2.left * speed * Time.deltaTime);
 }

 public void TakeDamage(int damage)
 {
     health -= damage;
     Debug.Log("damage taken");

     if (c == left)
     {
         transform.position = new Vector2(transform.position.x + knockBack, transform.position.y);
         Debug.Log("hit left side");
     }
     if(c == right)
     {
         transform.position = new Vector2(transform.position.x - knockBack, transform.position.y);
         Debug.Log("hit right side");
     }

     if (health <= 0)
     {
         Destroy(gameObject);
         Debug.Log("enemy is dead");
     }
 }

 void OnCollisionEnter2D(Collision2D collision)
 {
     if(collision.gameObject.name == "Player")
     {
         PlayerController.health--;
         Debug.Log("player is hit");
     }
 }

When I play the game as it is now, the player can deal damage to the enemy when the space key is pressed, and the "damage taken" message appears, but the game just treats thw two colliders like one, and the position of the enemy doesn't change. Any help would be appreciated ^^

Note: player and enemy objects in comments

Comment
Add comment · Show 9
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 Lineweaver · May 03, 2019 at 07:14 PM 0
Share

player object, red circle is the area where the attack happens: alt text

enemy object, with two different colliders: alt text

player-c.png (245.9 kB)
enemy-c.png (251.7 kB)
avatar image galactichyperstar · May 04, 2019 at 03:27 AM 0
Share

I'm confused. When and where are you setting the c Collider2D? I can't see it being set anywhere, so it's always equal to null.

avatar image Lineweaver galactichyperstar · May 04, 2019 at 11:38 AM 0
Share

The collider id defined in the Enemy script. I then added two box colliders 2d to the enemy object, and put them colliders slots in the inspector (the second picture).

avatar image Reedex Lineweaver · May 04, 2019 at 01:24 PM 0
Share

i tjink he means the "c" collider, that is , you're missing something like c = left/right. so its null all the time

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by galactichyperstar · May 04, 2019 at 08:30 PM

You are not telling the Enemy script which collider was hit. Remove the "c" Collider2D from the script and change the TakeDamage function to this:

 public void TakeDamage(int damage, Collider2D c) {
     health -= damage;
     Debug.Log("damage taken");
     if (c == left) {
         transform.position = new Vector2(transform.position.x + knockBack, transform.position.y);
         Debug.Log("hit left side");
     }
     if (c == right) {
         transform.position = new Vector2(transform.position.x - knockBack, transform.position.y);
         Debug.Log("hit right side");
     }
     if (health <= 0) {
         Destroy(gameObject);
         Debug.Log("enemy is dead");
     }
 }

Now, when you call the TakeDamage function, you need to provide which Collider2D was hit. In the PlayerAttack script, change it to:

 void Update() {
     if (timeBtwAttack <= 0) {
         if (Input.GetKey(KeyCode.Space)) {
             Collider2D[] enemiesToDamage = Physics2D.OverlapCircleAll(attackPos.position, attackRange, whatIsEnemies);
             for (int i = 0; i < enemiesToDamage.Length; i++) {
                 enemiesToDamage[i].GetComponent<Enemy>().TakeDamage(damage, enemiesToDamage[i]);
             }
             timeBtwAttack = startTimeBtwAttack;
         }
     } else {
         timeBtwAttack -= Time.deltaTime;
     }
 }
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 Lineweaver · May 05, 2019 at 10:22 AM 0
Share

It works now, thank you so much ^^

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

258 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 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

i can't get an object to respond to collision! (newbie question) 1 Answer

Colliders 2d apparently touch each other even if they should not. 2 Answers

Multiple Stacked Colliders only register for single object 0 Answers

2D Collider aren't exact? 6 Answers

Tilemap collider doesn't appear 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