How to do it...

To play sound and visual effects with Animation Events, follow these steps:

  1. Import the character with the Jump animation.
  2. In the Import SettingsAnimation tab, select the Jump animation.
  3. Make it loop.
  4. Go to the Events section.
  5. Scrub through the timeline in the Preview section and click on the Add Event button. The Edit Animation Event window will appear, as shown in the following screenshot:
Edit Animation Event window
  1. Type Sound in the Function field and Jump in the String field. This will call a Sound function in a script attached to the character and pass the Jump word as a string parameter to it.
  2. Create another Animation Event. Set the Function field to Effect and the String field to Dust.
  3. Apply the Import Settings.
  4. Create an Animator Controller for the character with just the Jump animation in it.
  5. Place the character in the scene.
  6. Attach the controller to the Animator component of the character.
  7. Attach an Audio Source component to the character.
  8. Uncheck the Play On Awake option.
  9. Create an empty game object and name it Dust.
  10. Add a Particle System component to it. This will be our dust effect.
  11. Set the Particle System's parameters as follows: 
    • Duration to 1 second
    • Start Life Time to 0.5 seconds
    • Start Speed to 0.4
    • Start Size to random between two constants: 1 and 2
    • Start Color to a light brown
    • EmissionRate to 0
    • EmissionBursts to one burst with Time set to 0, Min and Max set to 5
    • ShapeShape to Sphere
    • ShapeRadius to 0.2
    • Color Over LifetimeCreate a gradient for the alpha channel. In the 0% mark and 100% mark, it should be set to 0. In the 10% and 90% mark, it should be set to 255.
  12. Create a new Material and set the shader to ParticlesAlpha Blended.
  13. Drag and drop a transparent texture of a DustParticle.png into the Texture field of the Material.
  14. Drag and drop the Material into the RendererMaterial slot of our DustParticle System.
  15. Create a Resources folder in the project's structure. Unity can load assets from the Resources folder in runtime, without the need to reference them as prefabs.
  16. Drag and drop the Jump.ogg sound and the Dust game object into the Resources folder.
  17. Write a new script and name it TriggerEffects.cs.
  18. This script has two public void functions. Both are called from the Jump animation as Animation Events. In the first function, we load an Audio Clip from the Resources folder. We set the Audio Clip name in the Animation Event itself as the string parameter (it was set to Jump). When we successfully load the Audio Clip, we play it using the Audio Source component, reference to which we store in the source variable. We also randomize the pitch of the Audio Source to have a little variation when playing the Jump.ogg sound:
        public void Sound (string soundResourceName) { 

            AudioClip clip = (AudioClip)
            Resources.Load(soundResourceName); 

            if (clip != null) 
            { 
                source.pitch = Random.Range(0.9f, 1.2f); 
                source.PlayOneShot(clip); 
            } 
        } 
  1. In the second function, we try to load a prefab with the name specified as the function's parameter. We also set this name in the Animation Event (it was set to Dust). If we manage to load the prefab, we instantiate it, creating the dust effect under our character's feet:
        public void Effect (string effectResourceName) { 
            GameObject effectResource = 
            (GameObject)Resources.Load(effectResourceName); 
            if (effectResource != null) 
            { 
                GameObject.Instantiate(effectResource, 
                transform.position, Quaternion.identity); 
            } 
        } 
  1. Assign the script to our character and play the game to see the effect.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.36.221