- Home /
keeping the xp after death
im using the fps player script from the fps tutorial that i added a level systeme... but the thing is when i die i lose all exp,added Hp and level. how can i keep my exp hp and level from restarting????
here is my script:
var maximumHitPoints = 100.0; var hitPoints = 100.0;
var bulletGUI : GUIText;
var healthGUI : GUITexture;
var walkSounds : AudioClip[]; var painLittle : AudioClip; var painBig : AudioClip; var die : AudioClip; var audioStepLength = 0.3;
var guiTextHealth : GUIText; var guiTextEXP : GUIText; var accumulatedExperiencePoints : int = 0; var levelExpRequirements : int = 250; var currentLevel : int = 1; var levelUp = false ;
private var machineGun : MachineGun; private var rocketLauncher : RocketLauncher; private var healthGUIWidth = 0.0; private var gotHitTimer = -1.0;
var rocketTextures : Texture[];
function Update () { if(accumulatedExperiencePoints >= levelExpRequirements) {
accumulatedExperiencePoints -= levelExpRequirements;
currentLevel += 1;
levelUp = true;
}
if (levelUp){
levelExpRequirements *= 1.2;
maximumHitPoints +=250;
hitPoints = maximumHitPoints;
gameObject.Find("MachineGun").GetComponent(MachineGun).damage += 10;
levelUp = false;
}
guiTextEXP.text = accumulatedExperiencePoints+"/" + levelExpRequirements + "|" + currentLevel;
guiTextHealth.text = hitPoints+"/" + maximumHitPoints;
}
function Awake () { machineGun = GetComponentInChildren(MachineGun); rocketLauncher = GetComponentInChildren(RocketLauncher);
PlayStepSounds();
healthGUIWidth = healthGUI.pixelInset.width;
}
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.red, 2.0);
}
function LateUpdate () { // Update gui every frame // We do this in late update to make sure machine guns etc. were already executed UpdateGUI(); }
function PlayStepSounds () { var controller : CharacterController = GetComponent(CharacterController);
while (true) {
if (controller.isGrounded && controller.velocity.magnitude > 0.3) {
audio.clip = walkSounds[Random.Range(0, walkSounds.length)];
audio.Play();
yield WaitForSeconds(audioStepLength);
} else {
yield;
}
}
}
function UpdateGUI () { // Update health gui // The health gui is rendered using a overlay texture which is scaled down based on health // - Calculate fraction of how much health we have left (0...1) var healthFraction = Mathf.Clamp01(hitPoints / maximumHitPoints);
// - Adjust maximum pixel inset based on it
healthGUI.pixelInset.xMax = healthGUI.pixelInset.xMin + healthGUIWidth * healthFraction;
// Update machine gun gui
// Machine gun gui is simply drawn with a bullet counter text
if (machineGun) {
bulletGUI.text = machineGun.GetBulletsLeft().ToString();
}
}
Answer by PrimeDerektive · Feb 03, 2011 at 01:47 AM
Look into the DontDestroyOnLoad function: http://unity3d.com/support/documentation/ScriptReference/Object.DontDestroyOnLoad.html
Or save and retrieve relevant stats to PlayerPrefs: http://unity3d.com/support/documentation/ScriptReference/PlayerPrefs.html
Answer by Design3.com · Feb 03, 2011 at 01:36 AM
You can make guiTextEXP a static var and it should be preserved across scene loads.
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Trigger Respawn 3 Answers
Respawn Point respawning far away 2 Answers
[solved] respawn when destroyed 1 Answer
How do I get my player to die when light shines on him? 2 Answers