Game Development Reference
In-Depth Information
089 //------------------------------------------------
090 //Called when animation has completed playback
091 public void SpriteAnimationStopped()
092 {
093 //If not equipped then exit
094 if(!IsEquipped) return;
095
096 //Show default sprite
097 DefaultSprite.enabled = true;
098 }
099 //------------------------------------------------
100 }
Note Notice that, due to class inheritance, the Weapon_Punch class is using inherited variables, such as
CanFire , as though they were its own. No declaration for them is provided in the Weapon_Punch.cs file.
Line 10. The DefaultSprite public variable refers a SpriteRenderer component
to be used as the default, idle state for the weapon. This value should be
specified from the Unity Editor, via the Object Inspector, before running the
code. In short, whenever the weapon is equipped but not being fire, the
DefaultSprite will show at the bottom-middle of the screen.
Line 46. Notice that gamer input is read using a virtual button with the
Input.GetButton function, as opposed to reading directly from the keyboard
with Input.GetKeyDown . This allows input mapping to be changed without
breaking the code—a great technique for allowing the gamer customizable
controls.
Line 51. The Fire behavior is coded as a coroutine for resetting the CanFire
variable back to true after the recovery delay.
Line 59. The punch animation is initiated by sending a message to the
GameObject with SendMessage . This allows animation playback using the
SpriteAnimator component without the Weapon class ever needing to know the
data type or interface details of the SpriteAnimator! SendMessage is sometimes a
great way to establish relationships and interaction between classes. But it does
have performance implications, which are considered in the last chapter.
Physics and Damage Dealing
In Listing 6-5, the Weapon class ( Weapon_Punch.cs ) responded to gamer input and applies damage to
enemies within range of an attack whenever an attack is launched. The specific details of detecting
whether an Enemy is hit is handled using the physics system, and covers lines 62-82. These are
reproduced in Listing 6-6 and warrant further discussion.
 
 
Search WWH ::




Custom Search