Game Development Reference
In-Depth Information
function Awake () {
PlayStepSounds();
healthGUIWidth = healthGUI.pixelInset.width;
}
3. Use the
ApplyDamage
function for the
Player
object. Like other similar functions that
you have seen, this function applies damage to the
Player
object. It also plays pain
sounds so the player knows he or she is being damaged by an enemy or hazard. If the
hitPoints
variable reaches 0, the script calls the next function,
Die
.
function ApplyDamage (damage : float) {
if (hitPoints < 0.0)
return;
// Apply damage
hitPoints -= damage;
if (Time.time > gotHitTimer && painBig && painLittle) {
if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
audio.PlayOneShot(painBig, 1.0 / audio.volume);
gotHitTimer = Time.time + Random.Range(painBig.length * 2, r
painBig.length * 3);
} else {
audio.PlayOneShot(painLittle, 1.0 / audio.volume);
gotHitTimer = Time.time + Random.Range(painLittle.length * 2, r
painLittle.length * 3);
}
}
if (hitPoints < 0.0)
Die();
}
4. he
Die
function kills the player and disables all scripts attached to the
Player
object.
It also calls out to another script called
LevelLoadFade
, telling it to run the function
FadeAndLoadLevel
, which makes the game application reset the level to a stored state—
usually the scene's default state or a checkpoint stored by other scripts. You will apply
the
LevelLoadFade.js
script after working with this
FPSPlayer.js
script.
function Die () {
if (die)
AudioSource.PlayClipAtPoint(die, transform.position);
var coms : Component[] = GetComponentsInChildren(MonoBehaviour);
for (var b in coms) {
var p : MonoBehaviour = b as MonoBehaviour;
if (p)
p.enabled = false;
}
LevelLoadFade.FadeAndLoadLevel(Application.loadedLevel, Color.red, 2.0);
}
Search WWH ::
Custom Search