• 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 xandermacleod · May 20, 2013 at 03:49 PM · animationmecanimcurveanimationcurveanimationevent

Animation Events and Mecanim

Hi there,

I've heard that Unity doesn't have any inbuilt support for animation events for mecanim and instead people for now have to use Curves to do a check.

I'm not very savy though when it comes to looking up curve information so i was hoping someone could post some kind of example about how this might be done.


For my current project I am trying to make a boolean false at the end of a dodge animation. The idea is if a 'dodging' boolean is true, the animator will transition from a jog animation to a dodge animation. I have already sorted out my event which males the 'dodging' boolean true, and I have setup a curve called EndAnimDodge on my Dodge animation in the inspector's import settings which turns from 1 to 0 on the very last frame. But I dont know how to access that value (or when) to say "if EndAnimDodge == 0 then 'dodging' = false".

Should it be done in Update? LateUpdate? FixedUpdate?

And how do I access that curve?

I cant use the GetCurrentAnimatorStateInfo(0) stuff because once the animator has transitioned to Dodge, it has no way of getting out of dodge, and i need to reset the flag so that my character can dodge again.


Any help / alternative suggestions are much appreciated.

PS. C# ideally

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by TonyLi · May 20, 2013 at 05:15 PM

Unity's tutorial video has segment on using animation curves in scripting to adjust the character controller's size when jumping: http://video.unity3d.com/video/7362044/unity-40-mecanim-animation-tutorial

As an animation plays, the curve will update a float parameter. You can get the parameter using Animator.GetFloat(): http://docs.unity3d.com/Documentation/ScriptReference/Animator.GetFloat.html

Since it's a float, you should probably check for crossing the zero-axis instead of exactly zero. On the last frame, have the curve drop to -1. As a general-purpose approach, you can trigger events whenever the curve crosses the zero-axis. This way, you can have something like a sine wave to handle multiple footsteps, for example. Also, if the animation isn't playing, the curve won't cross the zero-axis, so it won't touch 'dodging'.

To check for crossing the axis, multiply the current value times the previous value. If it's < 0, the sign changed, and you can use SetBool() to set 'dodging' to true: http://docs.unity3d.com/Documentation/ScriptReference/Animator.SetBool.html

In Update(), something like:

 float previous = 0;
 
 void Update() {
     endAnimDodge = animator.GetFloat("EndAnimDodge");
     bool crossedZero = ((endAnimDodge * previous) < 0);
     if (crossedZero) animator.SetBool("dodging", true);
     previous = endAnimDodge;
 }
 



Or buy G1NurX's Event System for Mecanim and skip all that: http://u3d.as/content/g1nur-x/event-system-for-mecanim/3QP

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 xandermacleod · May 20, 2013 at 05:20 PM 0
Share

ah ya beat me to it. Cheers though!

avatar image
1

Answer by xandermacleod · May 20, 2013 at 05:20 PM

Ok, so I found out the solution but it took me a while.

First you have to add a parameter into your animator that shares the exact same name as your Curve.

After that you do something along the lines of:

 private animator MyAnimator;
 
 if(MyAnimator.GetFloat("parameterFloatName") < 1){
 //Do Other stuff
 }

The thing you'll also want to bare in mind is that depending on how your blending works, that parameter value will be affected. So for example you would probably want to do a debug.log every frame to check the parameter value as it might never reach the value you think it will reach. Also if you are doing the check every update, you will want to make sure your curve has a few frames worth of milliseconds spare, so that the system has a window of opportunity to check the curve value.

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.

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

14 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

Related Questions

Animator Jerky Animations 0 Answers

Extended Jump Animation Mecanim 0 Answers

Create a linear animation curve with javascript 1 Answer

Problem using mecanim and the Animation process 0 Answers

Get an Animator state inside an AnimationEvent callback of the last frame 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges