var deadReplacement : Transform;
var dieSound : AudioClip;
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0)
return;
hitPoints -= damage;
}
function (detonate)
// Destroy ourselves
Destroy(gameObject):
// Play a dying audio clip
(dieSound)
}AudioSource.Pl;ayClipAtPoin (dieSound); transform.position;
// Replace ourselves with the dead body
if (deadReplacement) {
var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) {
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
error Assets/WeaponScripts/CharacterDamage.js(23,18): UCE0001: ‘;’ expected. Insert a semicolon at the end.
I am having trouble. I know what I have to do but I’m not getting what to do. As you can see, there is a semicolon at the end of the line!
Boy, you’ve made a big mess in the middle of the script (the AudioSource.PlayClipAtPoint is completely messed). I think this will work:
var hitPoints = 100.0; // this is the health; when <= 0 the object dies
var deadReplacement : Transform;
var dieSound : AudioClip;
function ApplyDamage (damage : float) {
// We already have less than 0 hitpoints, maybe we got killed already?
if (hitPoints <= 0.0) return;
hitPoints -= damage; // subtract damage from health
if (hitPoints <= 0) { // if health <= 0 the object goes to the grave:
// Destroy ourselves
Destroy(gameObject);
// Play a dying audio clip
AudioSource.PlayClipAtPoint(dieSound, transform.position);
// Replace ourselves with the dead body
if (deadReplacement) {
var dead : Transform = Instantiate(deadReplacement, transform.position, transform.rotation);
// Copy position & rotation from the old hierarchy into the dead replacement
CopyTransformsRecurse(transform, dead);
}
}
}
static function CopyTransformsRecurse (src : Transform, dst : Transform) {
dst.position = src.position;
dst.rotation = src.rotation;
for (var child : Transform in dst) {
// Match the transform with the same name
var curSrc = src.Find(child.name);
if (curSrc)
CopyTransformsRecurse(curSrc, child);
}
}
I suppose this is the FPS Tutorial ApplyDamage function. When this object receives the message “ApplyDamage”, this routine is called. It subtracts the damage value from the object’s “health” (variable hitPoints). When the health become zero or less, the object “dies”: it plays its dieSound (if any) and if a dead replacement object was defined, it clones its children to the dead object before suiciding. The dead object is the dead version of the original one - burned, blasted, wrecked etc.