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 /
  • Help Room /
avatar image
0
Question by SeudoNimm · Jun 25, 2018 at 05:58 PM · unity 2dcollider2dattackhitbox

Boxcollider not dealing damage unless moving | Unity2D

I'm trying to set up an "attack" using boxcolliders, I've set up so that the collider attached to the player character becomes active when you press a button and is supposed to take health from a object tagged as enemy, but the enemy only takes damage when its moving into the active collider or vice versa, but not when stationary.

these are my scripts:

  public class PlayerAttack : MonoBehaviour {

     private bool attacking = false;
 
     private float attackTimer;
     private float attackCd = 0.3f;
 
     public Collider2D attackTrigger;
     
     void Start () {
         //turns off hitbox collider at start
         attackTrigger.enabled = false;
     }
     
     // Update is called once per frame
     void Update () {
         if(Input.GetKeyDown("k") && !attacking)
         {
             attacking = true;
 
             attackTimer = attackCd;
 
             //turns on hitbox collider if
             attackTrigger.enabled = true;
         }
 
         if (attacking)
         {
             if(attackTimer > 0)
             {
                 //subtracts to countdown the cooldown timer
                 attackTimer -= Time.deltaTime;
             }
             else
             {
                 //turns off hitbox collider after attack
                 attacking = false;
                 attackTrigger.enabled = false;
             }
         }
     }
 }
 

 public class AttackTrigger : MonoBehaviour {
 
     public int dmg = 20;
 
     private void OnTriggerEnter2D(Collider2D col)
     {
         if (col.isTrigger != true && col.CompareTag("Enemy"))
         {   
 
             col.SendMessageUpwards("Damage", dmg);
         }
     }
 
 
 }
 

 public class EnemyBehavior : MonoBehaviour {
 
     public float currHealth;
 
     // Update is called once per frame
     void Update () {
         if(currHealth <= 0)
         {
             Destroy(gameObject);
         }
     }
     
     public void Damage(int damage)
     {
         currHealth -= damage;
     }
 }



EDIT: Here is the movement script

 public class PlayerMovement : MonoBehaviour {
     public float movementSpeed;
     public float jump;
     public Rigidbody2D rb;
     public bool onGround = true;
     public float dblJmp = 0f;
     public float dashSpeed;
     public float dashTime;
     public float dashInc;
     public float dashMax;
     // Use this for initialization
     void Start () {
         
     }
 
     
     void Update() {
         //moves player right
         if (Input.GetKey("d") || Input.GetKey(KeyCode.RightArrow))
         {
             transform.Translate(Vector3.right * Time.deltaTime * movementSpeed);
     
             if (Input.GetKeyDown("j"))
             {
                 rb.AddForce(transform.right * dashSpeed);
                 rb.velocity = Vector3.zero;
             }
 
         }
         //moves player left
         if (Input.GetKey("a") || Input.GetKey(KeyCode.LeftArrow))
         {
             transform.Translate(Vector3.left * Time.deltaTime * movementSpeed);
     
             if (Input.GetKeyDown("j"))
             {
                 rb.AddForce((-1 * transform.right) * dashSpeed);
                 rb.velocity = Vector3.zero;
             }
         }
         //makes player jump and double jump and checks if player is on ground before allowing jump
         if ((Input.GetKeyDown("w") || Input.GetKeyDown(KeyCode.UpArrow)) && (onGround == true) /*&& (dblJmp < 2)*/)
         {
             //sets velocity of jumping player to zero before jump to make jump height consistent 
             rb.velocity = Vector3.zero;
     
                 rb.AddForce(transform.up * jump);
             dblJmp++;
         }
     
         if (dblJmp < 2)
         {
             onGround = true;
         }
         else
         {
             onGround = false;
         }
 
 
 
          }
     //checks if player is on the ground, and sets double jump counter to zero if on ground
     private void OnCollisionEnter2D(Collision2D collision)
     {
         if (collision.gameObject.CompareTag("Ground"))
         {
             dblJmp = 0f;
             onGround = true;
         }
     }
     private void OnCollisionExit2D(Collision2D collision)
     {
         if (collision.gameObject.CompareTag("Ground"))
         {
             onGround = false;
         }
     }
 }



Comment
Add comment · Show 1
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 SeudoNimm · Jun 28, 2018 at 05:40 PM 0
Share

I've used debug.log and it seems the problem has to do with the if statement in the AttackTrigger script, but I'm not sure what.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by binaryuniverse · Jun 25, 2018 at 11:01 PM

If you circumvent normal physics operations, you'll need to manually check interactions.

Use the Physics.Overlap functions (for example, Physics,OverlapBox, Physics.OverlapSphere)

Comment
Add comment · Show 8 · 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 SeudoNimm · Jun 28, 2018 at 05:39 PM 0
Share

I tried doing this but I'm running into the same problem, where nothing will happen unless the collider is moving.

I've used debug.log and it seems the problem has to do with the if statement in the AttackTrigger script, but I'm not sure what.

avatar image binaryuniverse SeudoNimm · Jun 30, 2018 at 09:53 PM 0
Share

I just noticed that you're calling the trigger function but expect the other object to NOT be a trigger (i.e. a regular collider)

If you don't have a rigidbody attached to at least ONE of these colliders, the collision will never register. Take a look at the collision matrix at the bottom of this screen, it may help you plan your next strategy.

https://docs.unity3d.com/$$anonymous$$anual/CollidersOverview.html

avatar image binaryuniverse SeudoNimm · Jun 30, 2018 at 09:55 PM 0
Share

Actually, I just noticed that you said the interactions DO occur when they move. Let me keep looking at your code. How do you move your objects? Do you use transform.position translate or do you use physics.addforce or change velocity?

avatar image binaryuniverse SeudoNimm · Jun 30, 2018 at 10:01 PM 0
Share

O$$anonymous$$, I think I might see the problem. You're looking for both objects to be stationary and for the attack to register, but you selectively turn on and off the collider. OnTriggerEnter functions only occur during entry. You might be missing that (time-wise.) You could try using OnTriggerStay ins$$anonymous$$d. The only detail is if you want discrete hits, you'll have to add a time counter to prevent hit spam$$anonymous$$g. So, register a hit immediately, then wait for e.g. 0.5 seconds before registering the next one (the time will depend on the constraints of your game.)

avatar image SeudoNimm binaryuniverse · Jul 03, 2018 at 09:39 PM 0
Share

So I've tried OnTriggerStay2D but it still does the same thing, the if statement doesn't go through.

I've added the movement script to the original question if that helps.

Show more comments

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

149 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

Related Questions

Overlapping colliders with Raycasting in 2D 0 Answers

Calculate BoxCollider2D based on the actual player sprite 2 Answers

Checking OnCollisionEnter2D names with TilemapCollider2D 0 Answers

2DCollider Issues 1 Answer

My ground checker only works sometimes 0 Answers


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