100 likes | 247 Views
Action Objects. Chapter 8. Colliders. Box Capsule Sphere Mesh Most expensive due to collision detection technique Convex meshes are most costly Provide a means of collision detection Act as means to trigger an event or reaction Item is can be selected with cursor
E N D
Action Objects Chapter 8
Colliders • Box • Capsule • Sphere • Mesh • Most expensive due to collision detection technique • Convex meshes are most costly • Provide a means of collision detection • Act as means to trigger an event or reaction • Item is can be selected with cursor • Collider does not have to math image exactly
Triggering Animation #pragma strict varaniParent : GameObject; varaniClip : AnimationClip; varaudioDelay : float = 0.0; function Start () { } function OnMouseDown () { print(name + " picked using " + aniClip.name); aniParent.animation.Play(aniClip.name); //check to make sure an Audio Source component exists before playing if (GetComponent(AudioSource)) { //yield new WaitForSeconds(audioDelay); //audio.Play(); audio.PlayDelayed (audioDelay); // delay before playing } }
Adding Sound F/X • Audio Listener • Acts as master switch to allow enable sound in a scene • Should only have one per scene • Every camera has an audio listener attached • Audio Source component • Added to GameObjects when sound needs to be emanated from specific locations • Attach audio source to camera or first person controller for background sound • Sound File • Loaded into the audio source component
Two-State Animation • Us Boolean variable to represent two states • Yield on line 51 allows for parallel operations • In his case a pause for the time necessary to play clipA
Two-state Animation #pragma strict vartwoStates : boolean = false; // default for single state objects varaniObject : GameObject; varaniClipA : AnimationClip; varaniClipB : AnimationClip; varaudioClipA : AudioClip; varaudioClipB : AudioClip; varaudioDelayA : float = 0.0; varaudioDelayB : float = 0.0; varaniLoopClip : AnimationClip; // if not null, this will be called after the first clip is finished internal varaniClip : AnimationClip; internal varfXClip : AudioClip; internal varaudioDelay : float; internal varobjState : boolean = true; // true is the beginning state, false is the second state function Start () { // if no parent was assigned, assume it is the object this script is on if (aniObject == null) aniObject = this.gameObject; } function OnMouseDown () { if (twoStates == false) objState = true; // if twoStates is false, set objectState to true if (objState) { // if objState is true/ use A aniClip = aniClipA; // set the new animation clip fXClip = audioClipA; // set the new audio clip audioDelay = audioDelayA; // set the new delay objState = false; // change its state to false } else { // the objState must be false / use B aniClip = aniClipB; // set the new animation clip fXClip = audioClipB; // set the new audio clip audioDelay = audioDelayB; // set the new delay objState = true; // change its state to true } print(name + " picked using " + aniClip.name); aniObject.animation.Play(aniClip.name); //check to make sure an Audio Source component exists before playing if (GetComponent(AudioSource)) { audio.clip = fXClip; // change the audio component's assigned sound file audio.PlayDelayed (audioDelay); // delay before playing it } if (aniLoopClip) { // wait the length of the first animation before you play the second yield new WaitForSeconds (aniClipA.length); aniObject.animation.Play(aniLoopClip.name); // this one needs to be set to loop } }
Unity’s Animation View • See page 286 • To remove key, have material fade out
Triggering Another Object's Animations • Set object to be animated in inspector
Limitations • Handles only two states • Trigger’s only one object’s animation/audio • Doesn’t manage visibility for objects that fade • Isn’t able to call other scripts or functions for special events
Summary In this chapter, you delved deeper into animation, learning how to trigger the legacy Animations and how to create your own clips using Unity’s Animation editor. You examined colliders, learning to replace resource-heavy Mesh Colliders with their more efficient primitive counterparts. With the addition of OnMouseDown(), you found that to pick an object in the scene, the object needs a collider. For mouse picks, using the Is Trigger option on objects that were too small to worry about collision was a good choice. With the introduction of animation.Play(), you found out how to call the default animations but soon learned that you could call specific animation clips by name, using the same function. On adding an Audio Source component, you could call sound effects with audio.Play() in the same manner, though calling the clip with audio.PlayOneShot() made your script more flexible. With the addition of yield WaitForSeconds(), you found that it delayed everything following it, so it was better to change the audio component’s audio clip directly and use PlayDelayed() instead. As the AniTwoState script got more robust, you learned you could check for a component’s existence with GetComponent() before trying to use it and thus avoid Null Reference Object errors. By the time you dealt with identifying parents with the Animation component, you found it was easy to adapt the script to call an object’s animations by picking a different object, a key concept for interactive games. Using Unity’s Animation editor, you learned how to create and add animation clips after import for non-animated objects. You also found you could add or edit imported animation clips, but only after making copies of them first. Finally, you saw the shortcomings in the AniTwoState script’s functionality with respect to future needs for the book’s adventure game project and found it somewhat lacking, even after all of the modifications made to it.