Search Unity

Health And Mana System With Progress Bar Script Donation

Discussion in 'Scripting' started by Wintergrasped, Mar 30, 2014.

  1. Wintergrasped

    Wintergrasped

    Joined:
    Dec 14, 2013
    Posts:
    19
    I got bored today and began making this script and decided to acully do somthing with it and give it out to people who may not know how to do somthing like this yet


    Code (csharp):
    1.  
    2. //       Script By: Wintergrasped         //
    3. //       A Donation from WMCSN.net        //
    4. // Licenced With Cretive Commons Atribute //
    5. //         No Credit Required             //
    6. //                                        //
    7. //                                        //
    8. //........................................//
    9.  
    10. static var curHealth : int = 100; //Curent Health static so other scripts can read and modify
    11. static var curmana : int = 150; //Curent mana static so other scripts can read and modify
    12. var manatext : GUIText;  //Print mana stats to the GUI text named manatext
    13. var healthtext : GUIText;
    14. var GUISkin : GUISkin;
    15. var currentHealth = curHealth;
    16. var maxHealth : int = 100;
    17. var healthregen : double = 1;
    18. var maxmana : int = 150; //Max amount of mana  
    19. var manaregenrate : double = 1;
    20. var manaregen : double = manaregenrate;
    21. var manabarLeft : int = 640;
    22. var healthbarleft : int = 640;
    23. var manabarverticle : int = 590;
    24. var healthbarvarticle : int = 620;
    25. var healthbarlength : int = 400;
    26. var manabarlength : int = 400;
    27. var WaitForBeforManaRegen : int = 2; //Time In Seconds
    28. var WaitForBeforHealthRegen : int = 2; //Time In Seconds
    29. var RegenHealthEvery : int = 1.0; //Time In Seconds
    30. var RegenManaEvery : int = 5.0; //Time In Seconds
    31. var AllowNegitiveMana : int = 0; //0 for false 1 for True
    32. var AllowMoreThenMaxHealth : int = 0; //0 for false 1 for True
    33. var AllowMoreThenMaxMana : int = 0; //0 for false 1 for True
    34.  
    35. InvokeRepeating("manaRegen", WaitForBeforManaRegen, RegenManaEvery );
    36. InvokeRepeating("healthRegen", WaitForBeforHealthRegen, RegenHealthEvery);
    37.      
    38.      
    39.      
    40.     function Start () {
    41.      
    42.      
    43.     healthRegen();
    44.      
    45.     }
    46.      
    47.      
    48.      
    49.      
    50.      
    51.     function Update () {
    52.    
    53.      
    54.      
    55.     manatext.text = curmana + " / " + maxmana;
    56.     healthtext.text = curHealth + " / " + maxHealth;
    57.      
    58.      
    59.     if (AllowNegitiveMana < 1){
    60.     if(curmana < 0 ) {
    61.      
    62.     curmana = 0;
    63.      
    64.      
    65.     }
    66.     }
    67.    
    68.      
    69.    if (AllowMoreThenMaxHealth < 1){  
    70.     if(curmana > 150) {
    71.      
    72.      
    73.     curmana = 150;
    74.      
    75.      
    76.     }
    77.      }
    78.      
    79.      //Base of an attack spell so that the attack spell script have one less thing to do
    80.     if(Input.GetKeyDown("1")) {
    81.      
    82.     curmana -= 10;
    83.      
    84.     }
    85.         if(Input.GetKeyDown("2")) {
    86.      
    87.     curmana -= 10;
    88.      
    89.     }
    90.         if(Input.GetKeyDown("3")) {
    91.      
    92.     curmana -= 20;
    93.      
    94.     }
    95.         if(Input.GetKeyDown("4")) {
    96.      
    97.     curmana -= 10;
    98.      
    99.     }
    100.         if(Input.GetKeyDown("5")) {
    101.      
    102.     curmana -= 15;
    103.      
    104.     }
    105.      
    106.      
    107.     if(curHealth < 0 ) {
    108.      
    109.     curHealth = 0;
    110.      
    111.      
    112.     }
    113.      
    114.     if (AllowMoreThenMaxHealth < 1){
    115.     if(curHealth > 100) {
    116.      
    117.      
    118.     curHealth = 100;
    119.      
    120.      
    121.     }
    122.      }
    123.      
    124.      //This Is for testing the Health and Mana system
    125.     if(Input.GetKeyDown("e")) {
    126.      
    127.     curHealth -= 10;
    128.     curmana -= 20;
    129.     }
    130.      
    131.      
    132.      
    133.     }
    134.      
    135.      
    136.      
    137.      
    138.     function healthRegen () {
    139.      
    140.     if(curHealth < maxHealth) {
    141.      
    142.     curHealth+=healthregen;
    143.      
    144.     }
    145.    
    146.      
    147.      
    148.     }
    149. function OnGUI()
    150. {
    151. GUI.HorizontalScrollbar(Rect (healthbarleft,healthbarvarticle,healthbarlength,0), 0, curHealth,0, maxHealth);
    152. GUI.HorizontalScrollbar(Rect (manabarLeft,manabarverticle,manabarlength,0), 0, curmana,0, maxmana);
    153. }
    154.    
    155. function manaRegen () {
    156.      
    157.     curmana += manaregen; //Add 5 mana to the curent mana
    158.      
    159.      
    160.      
    161.     }

    Extended Version With Health, Mana, Experiance, Level, and Economy Systems
    Code (csharp):
    1.  
    2. static var curHealth : int = 100;
    3. static var curmana : int = 150; //Curent mana static so other scripts can read and modify
    4. var manatext : GUIText;  //Print mana stats to the GUI text named manatext
    5. var healthtext : GUIText;
    6. var NoManaText : GUIText;
    7. var GUISkin : GUISkin;
    8. var LevelText : GUIText;
    9. var LevelUpText : GUIText;
    10. var NoCashText : GUIText;
    11. var currentHealth = curHealth;
    12. var maxHealth : int = 100;
    13. var healthregen : double = 1;
    14. var maxmana : int = 150; //Max amount of mana  
    15. var manaregenrate : double = 1;
    16. var manaregen : double = manaregenrate;
    17. var manabarLeft : int = 640;
    18. var healthbarleft : int = 640;
    19. var manabarverticle : int = 590;
    20. var healthbarvarticle : int = 620;
    21. var healthbarlength : int = 400;
    22. var manabarlength : int = 400;
    23. var WaitForBeforManaRegen : int = 2;
    24. var WaitForBeforHealthRegen : int = 2;
    25. var RegenHealthEvery : int = 1.0;
    26. var RegenManaEvery : int = 5.0;
    27. var AllowNegitiveMana : int = 0; //0 for false 1 for True
    28. var AllowMoreThenMaxHealth : int = 0; //0 for false 1 for True
    29. var AllowMoreThenMaxMana : int = 0; //0 for false 1 for True
    30. static var Strength : int = 0;
    31. static var Experiance : int= 0;
    32. static var Damage : int = 25;
    33. static var Level : int = 0;
    34. static var Intel : double = 0;
    35. static var Stamina : double = 0;
    36. static var Wisdom : double = 1;
    37. static var Balance : int = 0;
    38. static var Income : int = 10;
    39. static var Expense :int = 50;
    40. static var Price : int = 0;
    41. static var Fee : int = 0;
    42. static var WithDrawlAmmount : int = 0;
    43. static var MinDeposit : int = 5;
    44. static var XpTradeAmmount : int = 0;
    45. var StartingBalance : int = 1000;
    46. var XpToLevel2 : int = 5;
    47. var XpToLevel3 : int = 10;
    48. var XpToLevel4 : int = 20;
    49. var XpToLevel5 : int = 30;
    50. var XpToLevel6 : int = 40;
    51. var XpToLevel7 : int = 50;
    52. var XpToLevel8 : int = 60;
    53. var XpToLevel9 : int = 70;
    54. var XpToLevel10 : int = 80;
    55. var XpToLevel11 : int = 100;
    56. var XpToLevel12 : int = 120;
    57. var XpToLevel13 : int = 140;
    58. var XpToLevel14 : int = 160;
    59. var XpToLevel15 : int = 220;
    60. var XpToLevel16 : int = 260;
    61. var XpToLevel17 : int = 300;
    62. var XpToLevel18 : int = 350;
    63. var XpToLevel19 : int = 400;
    64. var XpToLevel20 : int = 450;
    65. var XpToLevel21 : int = 500;
    66. var XpToLevel22 : int = 550;
    67. var XpToLevel23 : int = 600;
    68. var XpToLevel24 : int = 650;
    69. var XpToLevel25 : int = 700;
    70. var XpToLevel26 : int = 750;
    71. var XpToLevel27 : int = 800;
    72. var XpToLevel28 : int = 850;
    73. var XpToLevel29 : int = 900;
    74. var XpToLevel30 : int = 1000;
    75. var XpToLevelElite : int = 1500;
    76.  
    77. //
    78. // REPEATING FUNCTION CALLS START HERE!
    79. //
    80.  
    81. InvokeRepeating("BillPay", 0, 3600);
    82. InvokeRepeating("Paycheck", 0, 3600);
    83. InvokeRepeating("manaRegen", WaitForBeforManaRegen, RegenManaEvery );
    84. InvokeRepeating("healthRegen", WaitForBeforHealthRegen, RegenHealthEvery);
    85.      
    86. //REAPEATING FUNCTION CALLS END HERE!    
    87.      
    88.     function Start () {
    89.      
    90.     healthRegen();
    91.      
    92.      
    93.     HideGUIText();  
    94.     //....
    95.     ApplyStats();
    96.     }
    97.      
    98.      
    99.      
    100.      
    101.      
    102.     function Update () {
    103.    
    104.    
    105.         if(Input.GetKeyDown("1")) {
    106.      
    107.     Experiance += 10;
    108.     Screen.showCursor = false;
    109.     }
    110.    
    111.    
    112.         if(Input.GetKeyDown("e")) {
    113.     if(curmana < 20){
    114.     Debug.Log("Not Enogh Mana");
    115.     ShowGUIText();
    116.     }
    117.     if (curmana > 20){
    118.     curHealth -= 10;
    119.     curmana -= 20;
    120.     }
    121.      }
    122.      
    123.      
    124.     if (AllowNegitiveMana < 1){
    125.     if(curmana < 0 ) {
    126.      
    127.     curmana = 0;
    128.      }
    129.      }
    130.  
    131. //
    132. //LEVEL SYSTEM STARTS HERE
    133. //
    134.  
    135. if (Experiance > 1) {
    136. if (Experiance > XpToLevel2) {
    137. Level = 2;
    138. Strength = 2;
    139. Intel = 2;
    140. }
    141. if (Experiance > XpToLevel3) {
    142. Level =3;
    143. Strength = 3;
    144. Intel =3;
    145. //....
    146. }
    147. if (Experiance > XpToLevel4) {
    148. Level =4;
    149. Strength =4;
    150. Intel =4;
    151. //....
    152. }
    153. if (Experiance > XpToLevel5) {
    154. Level =5;
    155. Strength =5;
    156. Intel =5;
    157. //....
    158. }
    159. if (Experiance > XpToLevel6) {
    160. Level =6;
    161. Strength =6;
    162. Intel =6;
    163. //....
    164. }
    165. if (Experiance > XpToLevel7) {
    166. Level =7;
    167. Strength =7;
    168. Intel =7;
    169. //....
    170. }
    171. if (Experiance > XpToLevel8) {
    172. Level =8;
    173. Strength =8;
    174. Intel =8;
    175. //....
    176. }
    177. if (Experiance > XpToLevel9) {
    178. Level =9;
    179. Strength =9;
    180. Intel =9;
    181. //....
    182. }
    183. if (Experiance > XpToLevel10) {
    184. Level =10;
    185. Strength =10;
    186. Intel =10;
    187. //....
    188. }
    189. if (Experiance > XpToLevel11) {
    190. Level =11;
    191. Strength =11;
    192. Intel =11;
    193. //....
    194. }
    195. if (Experiance > XpToLevel12) {
    196. Level =12;
    197. Strength =12;
    198. Intel =12;
    199. //....
    200. }
    201. if (Experiance > XpToLevel13) {
    202. Level =13;
    203. Strength =13;
    204. Intel =13;
    205. //....
    206. }
    207. if (Experiance > XpToLevel14) {
    208. Level =14;
    209. Strength =14;
    210. Intel =15;
    211. //....
    212. }
    213. if (Experiance > XpToLevel15) {
    214. Level =15;
    215. Strength =15;
    216. Intel =16;
    217. //....
    218. }
    219. if (Experiance > XpToLevel16) {
    220. Level =16;
    221. Strength =16;
    222. Intel =18;
    223. //....
    224. }
    225. if (Experiance > XpToLevel17) {
    226. Level =17;
    227. Strength =18;
    228. Intel =20;
    229. //....
    230. }
    231. if (Experiance > XpToLevel18) {
    232. Level =18;
    233. Strength =20;
    234. Intel =22;
    235. //....
    236. }
    237. if (Experiance > XpToLevel19) {
    238. Level =19;
    239. Strength =22;
    240. Intel =24;
    241. //....
    242. }
    243. if (Experiance > XpToLevel20) {
    244. Level =20;
    245. Strength =24;
    246. Wisdom =2;
    247. Intel =26;
    248. //....
    249. }
    250. if (Experiance > XpToLevel21) {
    251. Level =21;
    252. Strength =30;
    253. Wisdom =3;
    254. Intel =32;
    255. //....
    256. }
    257. if (Experiance > XpToLevel22) {
    258. Level =22;
    259. Strength =32;
    260. Intel =45;
    261. //....
    262. }
    263. if (Experiance > XpToLevel23) {
    264. Level =23;
    265. Strength =35;
    266. Intel =50;
    267. //....
    268. }
    269. if (Experiance > XpToLevel24) {
    270. Level =24;
    271. Strength =38;
    272. Intel =55;
    273. //....
    274. }
    275. if (Experiance > XpToLevel25) {
    276. Level =25;
    277. Strength =40;
    278. Wisdom =5;
    279. //....
    280. }
    281. if (Experiance > XpToLevel26) {
    282. Level =26;
    283. Strength =42;
    284. //....
    285. }
    286. if (Experiance > XpToLevel27) {
    287. Level =27;
    288. Strength =43;
    289. //....
    290. }
    291. if (Experiance > XpToLevel28) {
    292. Level =28;
    293. Strength =45;
    294. //....
    295. }
    296. if (Experiance > XpToLevel29) {
    297. Level =29;
    298. Strength =50;
    299. //....
    300. }
    301. if (Experiance > XpToLevel30) {
    302. Level =30;
    303. Strength =55;
    304. Wisdom =6;
    305. //....
    306. }
    307. if (Experiance > XpToLevelElite) {
    308. Level = 1000;
    309. Strength =100;
    310. Wisdom =100;
    311. Intel =500;
    312. //....
    313. }
    314.  
    315.  
    316. //LEVEL SYSTEM ENDS HERE!
    317.  
    318.  
    319.     }
    320.     }
    321.      
    322.    if (AllowMoreThenMaxHealth < 1){  
    323.     if(curmana > 150) {
    324.      
    325.      
    326.     curmana = 150;
    327.      
    328.      
    329.     }
    330.      }
    331.      
    332.      //
    333.      //KEY CODE LISTENERS START HERE
    334.      //
    335.      
    336.     if(Input.GetKeyDown("1")) {
    337.      
    338.     Experiance += 10;
    339.      
    340.     }
    341.          if(Input.GetKeyDown("4"))
    342.     {
    343.         CharacterMotor.maxForwardSpeed +=15;
    344.         CharacterMotor.maxGroundAcceleration +=15;
    345.         Debug.Log("Checked");
    346.     }
    347.         if(Input.GetKeyDown("2")) {
    348.      
    349.     curmana -= 10;
    350.      
    351.     }
    352.         if(Input.GetKeyDown("3")) {
    353.      
    354.     curmana -= 20;
    355.      
    356.     }
    357.         if(Input.GetKeyDown("4")) {
    358.      
    359.     curmana -= 10;
    360.      
    361.     }
    362.         if(Input.GetKeyDown("5")) {
    363.      
    364.     curmana -= 15;
    365.      
    366.     }
    367.    
    368.     //KEY CODE LISTENERS END HERE!
    369.      
    370.    
    371.      
    372.     //
    373.     //Anit-Negitave Health Starts HERE!
    374.     //
    375.        
    376.     if(curHealth < 0 ) {
    377.      
    378.     curHealth = 0;
    379.      
    380.      
    381.     }
    382.      
    383.     if (AllowMoreThenMaxHealth < 1){
    384.     if(curHealth > 100) {
    385.      
    386.      
    387.     curHealth = 100;
    388.      
    389.      
    390.     }
    391.     //ANIT-NEGITAVE HEALTH ENDS HERE!
    392.    
    393.      }
    394.      
    395.      //This Is for testing the Health and Mana system
    396.      
    397.      
    398.  
    399.      //
    400.      //REGEN ITEMS START HERE!
    401.      //
    402.      
    403.      function manaRegen () {
    404.    
    405.      if (curmana < 150){
    406.     curmana += manaregen; //Add 5 mana to the curent mana
    407.      }
    408.      
    409.      
    410.     }
    411.      
    412.     function healthRegen () {
    413.      
    414.     if(curHealth < maxHealth) {
    415.      
    416.     curHealth+=healthregen;
    417.      
    418.     }
    419.    //REGEN ITEMS END HERE!
    420.      
    421.      
    422.     }
    423.  //
    424. // GUI STUFF STARTS HERE!
    425. //  
    426. function OnGUI()
    427. {
    428. HealthsBar = GUI.HorizontalScrollbar(Rect (Screen.width - 358,Screen.height - 50,350,50), 0, curHealth,0, maxHealth);
    429. ManaBar = GUI.HorizontalScrollbar(Rect (Screen.width - 358,Screen.height - 70,350,900), 0, curmana,0, maxmana);
    430. GUI.Label (Rect (Screen.width - 200,Screen.height - 73,300,900), "Mana");
    431. GUI.Label (Rect (Screen.width - 200,Screen.height - 53,350,50), "Health");
    432. LevelText.text = "Level: " + Level  + " " +  "XP: " + Experiance;
    433. }
    434.    
    435. function ShowGUIText()
    436. {
    437. NoManaText.enabled = true;
    438. StopCoroutine("HideGUIText");
    439. StartCoroutine("HideGUIText");
    440. }
    441.  
    442. function HideGUIText()
    443.  
    444. {
    445. yield WaitForSeconds(1.3);
    446. NoManaText.guiText.enabled = false;
    447.  
    448. }
    449.  
    450. function NotEnoghCashShowGUIText()
    451. {
    452. NoCashText.enabled = true;
    453. StopCoroutine("NotEnoghCashHideGUIText");
    454. StartCoroutine("NotEnoghCashHideGUIText");
    455. }
    456.  
    457. function NotEnoghCashHideGUIText()
    458.  
    459. {
    460. yield WaitForSeconds(1.0);
    461. NoCashText.guiText.enabled = false;
    462.  
    463. }
    464.  
    465. function LevelGUIText()
    466.  
    467. {
    468. LevelUpText.enabled = true;
    469.  
    470. yield WaitForSeconds(1.0);
    471. LevelUpText.guiText.enabled = false;
    472.  
    473. }
    474. //GUI STUFF ENDS HERE!
    475.  
    476. //STAT SYSTEM STARTS HERE!
    477. function ApplyStats () {
    478.     maxmana +=Intel;
    479.     maxHealth +=Stamina;
    480.     maxHealth +=Wisdom;
    481.     maxmana +=Wisdom;
    482.     Damage +=Strength;
    483.     Damage +=Wisdom;
    484.     manaregenrate += Intel;
    485.     }
    486. //STAT SYSTEM ENDS HERE!
    487.  
    488. //
    489. //Economy System Starts Here!
    490. //
    491.  
    492. function Paycheck() {
    493.  Balance += Income;
    494. }
    495. function BillPay() {
    496.  if (Balance > Expense){
    497.  Balance -= Expense;
    498. }
    499. if (Balance < Expense) {
    500. NotEnoghCashShowGUIText();
    501.  }
    502.  }
    503. function Purchase() {
    504.  if (Balance > Price){
    505.  Balance -= Price;
    506. }
    507. }
    508. function FeeSytem() {
    509.  if (Balance > Fee){
    510.  Balance -= Fee;
    511.  }
    512.  }
    513. function Bal() {
    514. Balance = Balance;
    515. }
    516. function withdrawl() {
    517. if (Balance > WithDrawlAmmount){
    518. Balance -= WithDrawlAmmount;
    519. }
    520. }
    521. function MakeAccount() {
    522. if (Balance > MinDeposit){
    523. Balance -= MinDeposit;
    524. }
    525. }
    526. function XpToCash() {
    527. if (Experiance > XpTradeAmmount){
    528. Experiance -= XpTradeAmmount;
    529. Balance += XpTradeAmmount;
    530. XpTradeAmmount = XpTradeAmmount;
    531. Experiance = Experiance;
    532. }
    533. if (Experiance < XpTradeAmmount){
    534. Debug.Log("No XP");
    535. }
    536. }
    537.  
     
    Last edited: Apr 8, 2014