Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Stealthygolem · Nov 25, 2014 at 11:35 PM · c#collisionontriggerenterattackframe

Problem with trigger collider and damage function not hitting on the same frame

Hello! I am using a player character that has a child with a trigger object which checks if enemies are in the array and able to take damage, or outside of it and there is no enemy to apply damage to. The control for this trigger is the right analog stick for the player. So it changes direction according to the axis input. That code looks like this:

      void Update() {
      [...]
         float rightHorizontal = Input.GetAxis ("P2_RightStickHorizontal");
         float rightVertical = Input.GetAxis ("P2_RightStickVertical");
 
         if (rightHorizontal != 0f || rightVertical != 0f) {
             float angle = Mathf.Atan2 (rightVertical, rightHorizontal) * Mathf.Rad2Deg;
             transform.rotation = Quaternion.Euler (new Vector3 (0, 0, angle));
         }

This all works fine and dandy, but there is one rather big issue concerning applying damage:

If I turn my character around instantly with the right stick, that means the attack and trigger should happen at the same frame, but the attack is not triggered. For me to be able to damage the enemy, the trigger collider must already be over an enemy before the damage function gets called. The check for an enemy is an "OnTriggerEnter2D" and an "OnTriggerExit2D" for adding or removing enemy from list.

This is my damage function:

 void Damage () {
     float addedDamageRange = Random.Range (2.5f, 5.5f);
     float damageDealt = attackDamage + addedDamageRange + strength * 0.25f;
     
     float rightHorizontal = Input.GetAxis ("P2_RightStickHorizontal");
     float rightVertical = Input.GetAxis ("P2_RightStickVertical");
     
     GameObject _attackObject = GameObject.Find ("GeorgeAttack");
     ShockwaveAttackScript attackAnim = (ShockwaveAttackScript)_attackObject.GetComponent ("ShockwaveAttackScript");
     
     if ((rightHorizontal != 0f || rightVertical != 0f) && meleeAttackCD == 0f) {
         attackAnim.isAttacking = true;
         foreach (Collider2D enemy in colliderList) {
             EnemyHealth eh = (EnemyHealth)enemy.GetComponent ("EnemyHealth");
             eh.hpLost = damageDealt;
             eh.curhp -= damageDealt;
             if (!enemy || eh.curhp <= 0) {
                 colliderList.Remove (enemy.collider2D);
                 return;
             }
         }
         meleeAttackCD = 1f - agility * 0.008f;
         audio.PlayOneShot (meleeAttack);
     }else {
         attackAnim.isAttacking = false;
     }
 }

So, what I want to do here is to maybe add a minimal time to wait before the damage applies, or get the trigger collider to work just a frame faster(?). Considering the damage function is just called in the main update like this (below), I am confused on how to make things work:

     void Update () {
         [...]
         //MeleeAttack Method called
         Damage ();

And yes, this is all based on the same script, initially. Any ideas, hints or tips to what I could do would be greatly appreciated. It's like, if it only waited one frame it would probably apply the damage!

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

1 Reply

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

Answer by Stealthygolem · Nov 26, 2014 at 01:51 PM

I resolved this myself in the end. this is the changes I made:

I made a coroutine that would wait one frame and call the Damage function:

     IEnumerator waitOneFrame() {
         yield return 0;
         Damage ();
     }

Then the damage function was altered slightly, to trigger inside the Update function:

     void Update() {
         [...]
         //MeleeAttack Method called
         GameObject _attackObject = GameObject.Find ("GeorgeAttack");
         ShockwaveAttackScript attackAnim = (ShockwaveAttackScript)_attackObject.GetComponent ("ShockwaveAttackScript");
 
         if ((rightHorizontal != 0f || rightVertical != 0f) && meleeAttackCD == 0f) {
             attackAnim.isAttacking = true;
             StartCoroutine(waitOneFrame());
         }else {
             attackAnim.isAttacking = false;
         }
         [...]

This made the attack work! But, with a slight problem, that it applied twice. So i changed the Update to a LateUpdate, and it now applies only once.

I bother answering my question in hope that this can help someone in the future!

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

26 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

Related Questions

Preventing multiple collisions on same target 3 Answers

Multiple Cars not working 1 Answer

Problem with OnTriggerExit 1 Answer

Collision reaction not working 2 Answers

Could some one help me with this script... 5 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