• 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
1
Question by Vail · May 27, 2014 at 10:59 PM · animation2dgridmelee

Sync two animations on two different gameObjects

Hi,

I have a top down game, based on grid. Similar to chess. My problem is syncing animations on two different game objects. When two figures meets on neighbouring fields they should play animations of attack and hit until one of them will be killed. For now I using coroutine to handle the fight. In Update I find units, sets they state to busy and start fight coroutine.

     IEnumerator Fight(Cell attackerCell, Cell opponentCell, IFightable attacker, IFightable opponent)
     {
         attackerCell.Unit ().busy = true;
         opponentCell.Unit ().busy = true;
 
         int attackerPower = attacker.attack - opponent.defense;
 
         if (attackerPower > 0) {
             opponent.health -= attackerPower;
             attacker.animator.SetTrigger("Attack");
             yield return new WaitForSeconds(attacker.animator.GetCurrentAnimatorStateInfo(0).length);
             opponent.animator.SetTrigger("Hit");
             yield return new WaitForSeconds(opponent.animator.GetCurrentAnimatorStateInfo(0).length);
 
             if (opponent.health <= 0) {
                 attackerCell.Unit ().busy = false;
                 Grid.self.ClearCell(opponentCell);
                 yield break;
             }
         }
 
         yield return new WaitForSeconds(1f);
 
         int opponentPower = opponent.attack - attacker.defense;
         
         if (opponentPower > 0) {
             attacker.health -= opponentPower;
             opponent.animator.SetTrigger("Attack");
             yield return new WaitForSeconds(opponent.animator.GetCurrentAnimatorStateInfo(0).length);
             attacker.animator.SetTrigger("Hit");
             yield return new WaitForSeconds(attacker.animator.GetCurrentAnimatorStateInfo(0).length);
 
             if (attacker.health <= 0) {
                 opponentCell.Unit ().busy = false;
                 Grid.self.ClearCell(attackerCell);
                 GameControll.self.money += 100;
                 yield break;
             }
         }
 
         yield return new WaitForSeconds(1f);
 
         attackerCell.Unit ().busy = false;
         opponentCell.Unit ().busy = false;
     }

Unfortunately it not work. When attack animation is in the middle, hit animation starts and after few iterations I can't say what is going on.

Anyone have any suggestions?

Thanks in advice

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 mr_cakez · Feb 08, 2015 at 10:06 PM 0
Share

Add a debug line after setting the first animation trigger to see what the actual animation length is, or if the animation clip has changed yet. I'm not sure how much of a delay there is with a triggered anim transition, but perhaps it's possible the length that you're using in the WaitForSeconds function is not the value you want.

You could also wait for the normalizedTime to reach 1.0 after verifying the AnimationStateInfo is from the state you want.

0 Replies

· Add your reply
  • Sort: 

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

21 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

Related Questions

2D Animation does not start 1 Answer

How to detect collision of a wall with grid movement? 1 Answer

How would one make an animation play in the 2D engine with the press of a button? 0 Answers

Animation plays in preview mode but not max screen? 1 Answer

Animation not at correct position W/Video 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