• 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
2
Question by ColpColtran · Apr 03, 2017 at 02:59 PM · animationmecanim

Mecanim animation line up with in game logic?

In my top-down RPG, I have several enemies like Goblins, Trolls, Dragons,...

Each of them have two variables:

baseAttackDuration = float (how long in seconds does attack take, varies between enemies)
increasedAttackSpeed = float (attack speed modifier, may change during combat, also varies between enemies)

So when the character attacks, the duration of the attack equals to:
attackDuration = baseAttackDuration / increasedAttackSpeed

Question:
Now, how should I set Mecanim to play attack animation so the animation lines up with in game logic and lasts exactly attackDuration?

Comment
Add comment · Show 3
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 Mikael-H · Apr 03, 2017 at 03:35 PM 0
Share

Some things you could add to clarify this question is: Will different enemy types need to share animator state machines? Do the values need to be updated at runtime?

I'll still try to give you a valid answer, assu$$anonymous$$g thatfyou DO need to change at runtime which might be too advanced if you don't need it. I don't know you level of coding either so this might be a bit too advanced, seeing that you're new of the site.

avatar image ColpColtran · Apr 03, 2017 at 04:01 PM 1
Share

1) I was thinking about using override controllers for different types of enemies but havent decided yet since there are some obstacles (example: handling random animation). Dont be constrained by this, sharing/not-sharing controllers isnt really that much important for me atm.

2) Values might update at runtime, but not during animation. (example: enemies hit by Slow spell will have their attack speed reduced for some time, but if it happens in middle of an attack, it will apply for nex attack)

I am intermediate at coding and intermidiate with Unity and I certainly have no problem learning some new stuff, even if its hard. I am new here since I have been able to find most of the solutions to my problems on google, but I havent found 'clean' solution to this :)

avatar image Mikael-H ColpColtran · Apr 03, 2017 at 04:26 PM 0
Share

ok my solution described below will probably work for you then. You clarification in the comment can be used in an edit of the original question, might make it easier for others who want to help you out with better answers than $$anonymous$$e :)

1 Reply

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

Answer by Mikael-H · Apr 03, 2017 at 03:48 PM

As you can see in this thread you can connect an animation clip speed to an animator parameter. In the animator state machine select a state with a clip assigned and you should see this:

alt text In the image above you can see that there is a parameter called "MySpeed" in the layer, and this has been chosen as a speed multiplier for the animation clip in question. You'll need to tweak the values to fit your "duration". Depending on your scenario, if you just need to wait to a specific point in the clip to e.g. apply damage, I would only set the speed and setup an event in the animation clip. Add the event at the point where you want to apply damage as described in the manual here.

If you need to know how to set animator parameters in code read the manual on Animation Parameters

This is the easiest way to do it. If you need to have more direct control there is a button in the image with the text "Add Behaviour". This will allow you to add a behaviour to this animation state. In this behaviour you can override several events such as "OnStateEnter". See the manual on State Machine Behaviours and this tutorial.


stateparam.png (14.0 kB)
Comment
Add comment · Show 2 · 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 ColpColtran · Apr 04, 2017 at 07:53 AM 0
Share

Well I dont think setting the animations "by eye" to certain duration is a good practice.

Solving my problem by adding behavior is certainly a way how to approach it and this was my best candidate, but I was looking for a cleaner way because I think this is very dirty solution to this problem.

I will mark your answer as accepted as it seems there is no better way to handle this, but I am beco$$anonymous$$g deeply unsatisfied with $$anonymous$$ecanim since the thing I want isnt anything really special as scaling animation duration is common thing in modern RPGs like Diablo 3

avatar image Mikael-H ColpColtran · Apr 04, 2017 at 08:07 AM 0
Share

Well what I mean by setting it manually is actually to make it look better, you could just wait until the niamtion has finished of course, but then the damage might be delayed from when the enemy got hit.

Scaling animations used to be hard but is now quite simple with this method I think. Is there something missing for your use-case? If so, pleas let me know, there might be away to make it perfect! :)

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

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Twisted hand using Mecanim 0 Answers

Use Humanoid Animations with aditional bones 2 Answers

[Unity 4.3] Skinned legacy animations do not work 1 Answer

What is causing this problem with Mecanim? Please help! 2 Answers

Mecanim - changing animation clip speed, through script? 4 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