Hi guys.
I´m new to scripting but managed to create this cool one based on Square´s Chrono Trigger level up system.
The thing is the expBar. It grows bigger when exp is added to “curExp”.
so if you have curExp = 0 and expToLevel = 20, the bar lenght will be “0%”
and when curExp = 19 the bar lenght will be like almost “100%” (cos when 100% is reached the level changes)
When the level changes I want the barLenght to go down to “0%” again and start to grow from “0%”.
But when the level changes the bar is kept with the amount of curExp the player already had.
ex:
If expToLevel = 50 and curExp = 50, the level changes,
then the new ExpToLevel = 100 and stills curExp = 50,
the ExpBarLenght starts at “50%”.
Does anybody knows how to fix this, since the system uses loop to calculate the expToLevel?
I made a giant commet where the problem is and translated the important commets to English
(but kept my portuguese ones on it).
When it get it working I´ll share the codes.
To use it add this script to the player, select the healthBarEnabled
and click on the curExp variable on the inspector to raise it.
/*
# Usar em conjunto com DestroyText.cs
# 1 - Criar um Prefab de um "3D Text" e adicionar a ele o DestroyText.cs
# 2 - Colocar o Prefab no textPrefab no Inspector
# Vinicius Rezendrix
# 06/06/2012
*/
using UnityEngine;
using System.Collections;
using System;
public class LevelUpper3 : MonoBehaviour {
public Transform myTransform; //Transform do alvo Player
public int maxExp; // maxima experiencia jogador pode obter (total== 101560 on level 100)
public int expToLevel = 0; // experiencia necessaria para passar de level
public int curExp; // experiencia que o jogador tem atualmente
public int realExp; // experiencia real necessaria para passarde nível == ExpToLevel - curExp
public int maxLevel = 100; // Level maximo que o jogador pode atingir
public int curLevel = 1; // Level atual do jogador
private string displayLevel; // cria a tabela que monitora o level do player
// to break the expToLevel infinite loop
public bool changeLevel = true; // para fazer o expToLevel so aumentar em 1 por vez e nao ficar em loop infinito
// 3DTEXT Prefab
// public Transform textPrefab; // armazena o prefab do texto 3d
// define o tamanho das GUI.Window
private Rect windowRect = new Rect (20, Screen.height / 2, 210, 125);
#region ProgressBar
public bool experienceBarEnabled = true; //Se a barra de exp esta ativada
public float experienceBarLenght; //Tamanho da Barra de Experiencia
#endregion
/*
public void instantiateText(){
//Creates a 3dtext tht goes up when the level changes
// Cria um novo texto na cena que sobe e some, na posição do jogador
Instantiate(textPrefab, myTransform.position, myTransform.rotation);
}
*/
// Use this for initialization
void Start () {
/* //if there´s not a 3dtextPrefab added on the inspector, then display: "Add the 3dTEXT..."
// se não houver Prefab armazenado no inspetor na variavel "textPrefab"
if(!textPrefab){
Debug.Log("Add the 3DTextPrefab to LevelUpper.cs");
}
*/
// Don´t forget to add the TAG "Player" to the player GameObject
// NÃO ESQUECER DE ADICIONAR A TAG "PLAYER" AO JOGADOR NO MENU INSPECTOR
GameObject go = GameObject.FindGameObjectWithTag("Player"); // seleciona o GameObject com a TAG "Player" como alvo
myTransform = go.transform; // define o Transform do Jogador com a Tag "Player" como alvo
}
void OnGUI() {
//Level Window
// Janela com o Level
windowRect = GUI.Window (2, windowRect, LevelWdw2Function, "Display Level");
// Displays the Exp Bar as the exp is accquared
//ExperienceBar - para mostrar uma barra de progresso conforme a exp é adquirida
if (experienceBarEnabled == true) {
GUI.Box(new Rect(20, Screen.height - 40, experienceBarLenght , 20), curExp + "/" + maxExp);
}
}
void LevelWdw2Function (int windowID) {
// Displays the Level Info inside "Display Level" window
//exibe o level dentro de uma caixa na janela "Display Level"
GUI.Box(new Rect(5, 20, 200, 100), displayLevel);
GUI.DragWindow(); // faz a janela "dragavel"
}
// Update is called once per frame
void Update () {
// Display Level Window Info
displayLevel = "Cur Level: " + curLevel +
"\n Exp To Level: " + expToLevel +
"\n Cur Experience: " + curExp +
"\n Experiencia a adquirir: " + realExp;
// recalculates the level and expToLevel
levelModifier();
/* !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
THE PROBLEM MIGHT BE DOWN HERE!
ON THIS experienceBarLenght line
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!*/
// Sets the ExpBarLenght
//Define o tamanho da Barra de Experiencia
experienceBarLenght = (Screen.width - 40) * (curExp / (float)expToLevel);
}
public void levelModifier() {
// bool to make the level to be calculated only once and end the infinite loop
if(changeLevel){
/* # Loop que calcula o valor do "expToLevel", ou seja, a experiencia total
# que o jogador deve obter para passar de level */
// Loop that sets the exp needed for the player go to next level
for(int i=0; i < curLevel; i++) {
expToLevel = expToLevel + ((curLevel + 1) * 10) - (curLevel -1) * 10;
changeLevel= false; //Breaks the infinite loop
}
}
//The real exp needed to go to next level
//after the script configs get ok, put it inside the if block down ahead
/* # experiencia real a ser adquirida para passar de nível [diferença de "ExpToLevel" menos "curExp"]
# depois de tudo certo, colocar dentro do IF para otimizar memoria */
realExp = expToLevel - curExp;
maxExp = expToLevel;
// If curExp reaches expToLevel...
// Se o curExp alcançar o ExpToLevel...
if(curExp >= expToLevel curLevel< maxLevel ) {
curLevel++; //...raises the level //...aumenta o level em 1
changeLevel = true; //activates the change level loop once more
/* //displays "Level Up" once
bool levelUp = true; // mostra o texto "levelUp" somente 1 vez
if(levelUp){
// Armazena o DestroyText.cs do Prefab de texto 3D na variavel "dt"
TextScroller ts = textPrefab.GetComponent<TextScroller>();
ts.textToDisplay = "Level Up!"; // muda a variavel "textToDisplay"
ts.whiteText(); //muda a cor do texto para branco
instantiateText();
levelUp = false; // muda o levelUp para false para só mostrar o texto "Level Up!" 1 vez
}
*/
}
//region nor BIGGER nor LESSER than
#region NAO MAIOR, NEM MENOR QUE...
// prevents curExp from going over maxExp
// evita que a curExp exceda o valor da maxExp
if(curExp > maxExp){
curExp = maxExp;
}
// prevents curLevel to go over the maxLevel
// evita que o curLevel exceda o valor do maxLevel
if(curLevel > maxLevel){
curLevel = maxLevel;
}
// prevents curLevel from going under 1
// evita que o curLevel fique abaixo de 1
if(curLevel < 1) {
curLevel = 1;
}
#endregion
}
}