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 /
avatar image
1
Question by LaurynasLubys · Mar 27, 2016 at 11:29 AM · eventsanimationeventanimation layers

Animation layers, duplicate AnimationEvents. (SOLVED)

Hey, this was asked before, but I cannot see if this was answered ever since 2010 in this forum. I have two layers of animation, 0 for normal animations and 1 for "player exhausted" animations.

I'm calling events on 0 layer to play foot step sounds and it works great. However, when I switch to layer 1 via animator.SetLayerWeight(1, 1); events from layer 0 is still being called all the time, so I get duplicate sounds.

  • I tried setting layer 0 to 0, same result;

  • Layer 1 is overriding 0 and opposite.

  • Tried checking what layer has higher weight value, and only then playing sounds. (check is ignored, since both layers are playing?)

Any solution to this, workaround ? Maybe using events is a bad idea, and I should call function to play sounds on collision enter ?

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 Kutiman · Mar 27, 2016 at 03:16 PM 0
Share

Can you add the code for the sound events?

3 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by LaurynasLubys · Mar 28, 2016 at 10:30 AM

I solved it. I noticed that both layers are always playing no matter the conditions (layer weights, bools etc) So I created an empty state in layer 0 and marked it as default. (which does not have any animation - therefor no sounds events are called) Then when character is exhausted, "tired", it switches to that empty animation state and stays on it, unless character is no longer exhausted. In this case, when character is exhausted and in layer 1, "tired", layer 0 is in that empty state without animation events ever called. alt text


animation-layers-animationevents-unity-bug-fix.jpg (146.4 kB)
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 monsterbluesHome · Sep 29, 2020 at 08:21 PM 0
Share

Thanks @lauris47 very practical no code solution.

avatar image
0

Answer by LaurynasLubys · Mar 27, 2016 at 08:34 PM

         public void PlaySounds(string name)
         {
             if (m_Animator.GetLayerWeight(1) == 1 && isExhausted)
             {
                 //will call AnimationEvents of layer 0 in adition to layer 1
                 if (name == "steps")
                 {
                     //playsound
                 }
             }
             if (m_Animator.GetLayerWeight(0) == 1 && !isExhausted)
             {
                 //will call only layer 0 events
                 if (name == "steps")
                 {
                     //playsound
                 }
             }
         }


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
avatar image
0

Answer by Kutiman · Mar 28, 2016 at 12:57 AM

Read here: http://docs.unity3d.com/Manual/animeditor-AnimationEvents.html

using this, you simply go to your walking animation, and in the inspector, go down to events and click the arrow to open it, slide with the red marker exactly to the frame of when the foot touches the floor and add an event there. Do this for every place a foot touches the floor in your animation. The small dialog box will open and simply write the name of the function that is in the script. example PlayStepSound, but only the name with no ().

That's it. You can do it with only one function, and the rest from the animation event dialog.

 public function PlayStepSound () {
     //Play sound
 }

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 LaurynasLubys · Mar 28, 2016 at 09:14 AM 0
Share

That is what I'm doing already. Problem is - no matter the conditions, both sounds are playing. When I run the character, and I am not exhausted and in layer 0 of animations, everything is fine.

I have a function, to detect if character is exhausted, in it I change state of exhausted to true. When it is true, I switch layer weights of 0 to 0 (not exhausted layer of animation) and 1 to 1 (exhausted layer). I have public bool isExhausted, and I print out layer wights in inspector. So I know the conditions switch 100%. However when I am in layer 1, and its weights are 1 and layer 0 has wights 0 I can still hear footsteps of layer 0, even when code does not allow to enter if statement, it does somehow. I think it is a bug in Unity, people just somehow go around it.

I could solve this stupidly by calling event by different string name, (footStepsNormal and footStepsExhausted) and then I would duplicate the code for playing foot steps sound. Just like it was solved here: http://answers.unity3d.com/questions/755073/how-to-ignore-anim-events-on-layers-with-weight-se.html

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

41 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

Related Questions

How do I call an animation event on a deactivated gameobject in unity3d c# 0 Answers

AnimationEvents - Get All Events? 0 Answers

Can one Animation Event be used for a timespan (multiple frames)? 1 Answer

How to create animation event at specific keyframe at runtime 2 Answers

Mecanim events firing incorrectly when clip playing backwards? 1 Answer


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