swaying tree with sound

Hi there, I have an idea and I am curious on how to approach this.

I might take a gander at making a tree that sways to a song soon. I did something similar before, with Max/MSP/Jitter communication but I do not want to use any third party apps this time (want to make it a web app).

Here are my ideas on how to approach the problem: -write down the ups and downs in the song on a piece of paper and timemap them in code. as in:

Option 1: -----horrible pseudo-code warning---------




I am all for this, I wouldn't mind writing down a 5 minute song like that, no problem. But I am sure there's a better way :)

Option 2:

Do the animation in a 3D app, export the mesh, voila!

Problem with the second approach is that, I would use time data to make leaves fall, apply image effects such as blur etc. as well, so that goes out of the window since there is now, no data to work with.

Also, Is it possible to break a model down to little pieces at runtime? Like, leaves are attached on import, and are detached at certain times? (I know I can do this in the 3D app as well - where's the fun in that?)

Thank you for the attention :)

Maybe you could just store this info in a midi file and read it during runtime? That would surely ease the "writing down" process. You could then use the midi events as a trigger for your events.

Yes you can break down the model into pieces. Easiest way would be to model the tree accordingly and then just do: leave.transform.parent = null; You could also do this procedurally, but then you'd have to know the exact vertices of your "connections" between leave and branch.

Remember, unity has a built-in animation editor which you can use to animate any publicly exposed variable.

If you create an animation of a single "sway" of the tree simply from one side to another, you can then use unity's animation editor to animate the AnimationState.time variable of your tree, so that you can scrub backwards and forwards through this animation in time with your music.

If you want to get information about how fast the tree is currently moving (for blur, shedding leaves, etc), you could use a variable to compare how much the current AnimationState 'time' values has changed since the previous frame.

You can then also attach events to any point during your animation which can trigger calls to scripts on your objects.