Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJefferson Lessa
5,684 PointsIs there a way to link the sounds to the actual animation?
I realize that is likely very tricky to do, but I am not very found of the idea of choosing a number that "feels right" and play the sound in that interval. If I ever had to change the animation, I would need to pick a new number. That also is a problem in other similar cenarios, ie. I want to apply damage effects on an enemy when my fighter's animation swing the sword, not when the animation starts.
I don't know if there is a better way of doing this. Can someone help?
2 Answers
John Shoffner
1,514 PointsHi Jefferson,
Not sure if you're still looking for an answer but....
To clarify. You would like to run an audio clip when the player's sword makes contact with an enemy? This concept is similar to the exercise of adding audio when the frog collects a fly (fly particles prefab), which is covered in the "Additional Audio Scripting" video (start at the 6:50 time mark). When the frog collects a fly, the fly particles animation is invoked; the audio source component was added to the fly particle prefab as the audio indicator, to the user, that the fly was collected. Similarly in your game, add an audio source to the game object that monitors for a character collision (player vs enemy). Another method would be to play an audio clip directly in code, if that's a preferred method. Playing an audio sound in code is demonstrated in the "Script a Random Sound Player" video. NOTE: There's no need to add the randomizing code, unless you have more than one possible sound when an enemy is hit.
HTH
Gavin Ralston
28,770 PointsPerhaps a Compound Collider would be more suitable, where both the damage and the sound are only applied once they're actually in contact.
Jefferson Lessa
5,684 PointsJefferson Lessa
5,684 PointsI see. Thanks for the input, but there is already a collision on the way. The collision would trigger an animation, and the effect should happen mid-animation.
The wait-a-few-sec solution fixes my problem, but I was hoping there was a better way of doing things. Guess there is not. But thanks a lot for your reply!