hello, im rather newish to unity i work in torque mostly, basically i used a bastardised version of the fpsplayer script to set me health to 100, attached to the player ofc. Now i have a model that is to repersent an enemy, and on collision it applys damage then destroys it self.
function OnCollisionEnter( collision : FPSPlayer )
{
FPSPlayer.hitPoints-=100;
Destroy(this);
}
How do i refrence the hitpoints as this script is attached to the enemy. This has an error asking for an instance of FPS so i tried
FPSplayer = new player
player.hitPoints -= 100;
but that didnt work either and would create a new player
function OnCollisionEnter (collisionInfo : Collision) : void
When OnCollisionEnter is called, it is automatically assigned a Collision variable. You must not change that.
So, you get a Collision variable, which contains informations…
Like gameObject, collider, rigidbody (to refer to the gameObject itself, or the Collider component of the gameObject, or the Rigidbody component of the gameObject.
yes and no, very occasionally it will fire, atm its not tho, i did have a refrence error witht he damage however when it did fire and was attached to the player insted it would delete everything on screen
This is FPSPlayer which i’ve changed a little to give u some idea
var maximumHitPoints = 100.0;
var hitPoints = 100.0;
var walkSounds : AudioClip;
var painLittle : AudioClip;
var painBig : AudioClip;
var die : AudioClip;
var audioStepLength = 0.3;
private var gotHitTimer = -1.0;
var rocketTextures : Texture[];
function ApplyDamage (damage : float) {
if (hitPoints < 0.0)
return;
// Apply damage
hitPoints -= damage;
// Play pain sound when getting hit - but don't play so often
if (Time.time > gotHitTimer painBig painLittle) {
// Play a big pain sound
if (hitPoints < maximumHitPoints * 0.2 || damage > 20) {
audio.PlayOneShot(painBig, 1.0 / audio.volume);
gotHitTimer = Time.time + Random.Range(painBig.length * 2, painBig.length * 3);
} else {
// Play a small pain sound
audio.PlayOneShot(painLittle, 1.0 / audio.volume);
gotHitTimer = Time.time + Random.Range(painLittle.length * 2, painLittle.length * 3);
}
}
// Are we dead?
if (hitPoints < 0.0)
Die();
}
function Die () {
if (die)
AudioSource.PlayClipAtPoint(die, transform.position);
// Disable all script behaviours (Essentially deactivating player control)
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.white, 2.0);
}
function PlayStepSounds () {
var controller : CharacterController = GetComponent(CharacterController);
while (true) {
if (controller.isGrounded controller.velocity.magnitude > 0.3) {
audio.PlayOneShot(walkSounds, 1.0 / audio.volume);
yield WaitForSeconds(audioStepLength);
} else {
yield;
}
}
}
First Person Controller : has FPSPlayer, and detection collision script,
Objects with tag “evil”.
When First Person Controller touches an object with tag evil, it triggers the detection collision script, and call Apply Damage to First Person Controller.