using System; [System.Serializable] public class Level { public int experience; public int currentLevel; public Action OnLevelUp; public int MAX_EXP; public int MAX_LEVEL = 99; public Level(int level, Action OnLevUp) { MAX_EXP = GetXPforLevel(MAX_LEVEL); currentLevel = level; experience = GetXPforLevel(level); OnLevelUp = OnLevUp; } public int GetXPforLevel(int level) { if (level > MAX_LEVEL) return 0; int firstPass = 0; int secondPass = 0; for (int levelCycle = 1; levelCycle < level; levelCycle++) { firstPass += (int)Math.Floor(levelCycle + (300.0f * Math.Pow(2.0f, levelCycle / 7.0f))); secondPass = firstPass / 4; } if(secondPass > MAX_EXP && MAX_EXP != 0) return MAX_EXP; if(secondPass < 0) return MAX_EXP; return secondPass; } public int GetLevelForXP(int exp) { if(exp > MAX_EXP) return MAX_EXP; int firstPass = 0; int secondPass = 0; for (int levelCycle = 1; levelCycle exp) return levelCycle; } if(exp > MAX_LEVEL) return MAX_LEVEL; return 0; } public bool AddExp(int amount) { if(amount+ experience < 0 || experience > MAX_EXP) { if(experience >MAX_EXP) experience = MAX_LEVEL; return false; } int oldLevel = GetLevelForXP(experience); experience += amount; if(oldLevel < GetLevelForXP(experience)) { if(currentLevel < GetLevelForXP(experience)) { currentLevel = GetLevelForXP(experience); if(OnLevelUp != null) OnLevelUp.Invoke(); return true; } } return false; } } Tnx for, Coding With Unity!
@danielsalyi71944 жыл бұрын
also I think it needs a little work around regarding the playerLevel: public void OnLevelUp() { print("levelUp"); int oldEXP = level.experience; int newexp = level.GetXPforLevel(level.currentLevel); level.experience = 0; level.experience = (oldEXP - newexp); } So it makes the experience to 0 and adds the leftover xp from the prev level
@CodingWithUnity4 жыл бұрын
awesome job! Thanks for posting :)
@ExileClips1234 жыл бұрын
Thank you!
@KaganParlatan4 жыл бұрын
Thanks Daniel
@mikethegamedev4 жыл бұрын
...
@CodingWithUnity3 жыл бұрын
New game! Take a shot each time I say "level up" and see if you can finish the video
@NiclasGleesborg05 жыл бұрын
Great video. It's a really good system.
@ashfid89875 жыл бұрын
So much better than listing arrays for each experience to next level. Thanks man! Will you in future make a video on inventory system? A basic one would be nice.
@CodingWithUnity5 жыл бұрын
It is a lot easier to use than an array list for sure! Thats actually my next series! I was planning on making a scriptable object based inventory system, and i might make a banking system video also
@ashfid89875 жыл бұрын
Coding With Unity shit, I am so excited for the inventory system haha. Scriptable Object is too good.
@kapkoder40094 жыл бұрын
Thanks for this! Actions are also something pretty cool to take away from this video :)
@FyresGames3 жыл бұрын
It work very well. I did another level up system before with very similar result, the main difference was each experience to next level was stored in a array setted in the start based onto the max level, experience for level 1 and a multiplity to next level. Both give the same result, just different way.
@CodingWithUnity3 жыл бұрын
awesome!
@leventocakci2 жыл бұрын
how can i implement this to using database version
@Golem1988 Жыл бұрын
How does one also display exp required to get the next level and how does one update that value on level up?
@TrevorPinkney4 жыл бұрын
For anyone looking to display this level as text in their game, and save the level so that it does not reset every game session, here is what I did in my game (On the script attached to your player in the inspector, the 'Leveltext' is where you will drag in your text gameobject from your hierarchy that is going to be used to display your player's level).This script is setup so that your level and experience will be permanently saved and can be accessed/modified and displayed anywhere else you want in your game. PlayerPrefs is the name in which the specified value will be saved(You can name the value in the quotation marks whatever you'd like, just make sure you remember it and type it exactly that way wherever else you'd like to access it. So in my script, PlayerPrefs.GetInt("Level") will use the Level value that is currently saved to my player. Hope this helps! using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; public class Player : MonoBehaviour { public Level level; public int currentxp; public Text Leveltext; //Start is called before the first frame update void Start() { currentxp = PlayerPrefs.GetInt("experience"); level = new Level(PlayerPrefs.GetInt("Level"), OnLevelUp); PlayerPrefs.SetInt("experience", PlayerPrefs.GetInt("experience") + 100); Leveltext.text = PlayerPrefs.GetInt("Level").ToString(); } public void OnLevelUp() { PlayerPrefs.SetInt("Level", PlayerPrefs.GetInt("Level") + 1); } //Update is called once per frame void Update() { if (Input.GetKeyDown(KeyCode.Space)) { level.AddExp(100); PlayerPrefs.SetInt("experience", PlayerPrefs.GetInt("experience") + 100); currentxp = PlayerPrefs.GetInt("experience"); Leveltext.text = PlayerPrefs.GetInt("Level").ToString(); } } }
@TrevorPinkney Жыл бұрын
@@Kyle-bc8pf Hey, the problem is that you can't change PlayerPrefs. PlayerPrefs is a command for unity to save information for the player. Unity is giving you that error because SaveScript is not recognized by unity. Keep PlayerPrefs as PlayerPrefs and everything will work just fine ☺️
@scott_33364 жыл бұрын
Thanks, I am new to Unity and C# and only know the basics of python si I am trying to get used to all the scripting stuff.
@general_md4 жыл бұрын
How do I put the values of the XP on a slider?
@sambell6755 жыл бұрын
I see you used a Runescape example on your last video. Any chance you'd be able to go into individual skills as seen in OSRS such as Attack, Strength, Defence, Woodcutting, Fishing etc and how your levelling up system would function between the separate skills? :)
@CodingWithUnity5 жыл бұрын
Sure just make a public Level Attack; public level Strength; public level defence; and so on :)
@redbeardjunior4 жыл бұрын
But what if you want to do the exp bar in front of the screen The script wil count up the xp and stay at a full bar leght because your counter is not reset to 0 any change you will make a new video?
@simaanakbar95505 жыл бұрын
hey there! nice video, i wanted to know how do I change my scenes, going from one stage to another based on the player level xp?? please help me out
@CodingWithUnity5 жыл бұрын
Did you get this working? Cant remember, pretty sure you asked on discord
@fantasymagic21084 жыл бұрын
The vendetta voice, thanks for the tutorial
@ExileClips1234 жыл бұрын
Hey, i made everything work from start to finish, i could add exp and level up from using spacebar, but for some reason i just cant imploment it on fx when my enemies die, i added this to my enemy script "public Level level;" and in a if function for if enemy health is 0 then it ofc dies but the i did "level.AddExp(100); but i dont get any exp from killing enemies?
@ExileClips1234 жыл бұрын
i know im just missing something basic, i always find it hard to use stuff from one script into another
@CodingWithUnity4 жыл бұрын
Adding `public level level` still requires you attach the level class on your player script to the enemy. Absolute easiest way i cant think to do it is, make a `public Player player` on your enemy and drag your palyer into that variable, then say player.level.AddExp() Then a way to not have to drag and drop the player into every enemy is make an EnemySpawner.cs script and spawn the enemies from that and pass the player into the enemy script when you spawn it. (can look up the factory pattern for more info on that)
@ExileClips1234 жыл бұрын
@@CodingWithUnity thanks for the reply, I ended up switching my code up a bit and also using another tutorial that went more in depth on doings stuff like getting exp from killing enemies and such, so made it work, but thanks 👍👍
@CodingWithUnity4 жыл бұрын
@@ExileClips123 Awesome! Glad you got it working!
@satisfyingworld4195 жыл бұрын
hello mate , do u know anyway i can save the current level when i restart the game becuase everytime restart the game i get level down to 1
@CodingWithUnity5 жыл бұрын
look into serializing classes, its pretty strait forward!
@DigitalPianoClassical4 жыл бұрын
Hi. Useful tutorial, with good detail and at just the right... level....(ahem) for an "intermediate amateur" like me. But I'm left wondering...why not use a lookup table?
@CodingWithUnity4 жыл бұрын
Thats kinda the next step, use this formula to populate a lookup table :)
@bistou4864 жыл бұрын
How would we go about attaching that to a ui ? and with that would it be possible to show the exp needed for next lvl ? like a bar growing and showing how close to a new lvl we are ? how about losing exp upon death but not going down a lvl, is the 10% growth easily modifiable ?
@CodingWithUnity4 жыл бұрын
Linked you to it in discord!
@ExileClips1234 жыл бұрын
@@CodingWithUnity can you link here? Also interested
@CodingWithUnity4 жыл бұрын
@@ExileClips123 Just follow the brackeys HP bar tutorial and replace the HP values with the exp values
@ExileClips1234 жыл бұрын
@@CodingWithUnity now that's smart, thanks xD
@Hazzardous20054 жыл бұрын
@@CodingWithUnity I followed the tutorial and made the slider but how would I use the code so that when I add xp the bar moves and resets after reaching the next level? sorry I don't have much knowledge on coding.
@Icey_Editss5 жыл бұрын
So you know a cash on kill right? But what if instead of cash it’s exp will it still work
@CodingWithUnity5 жыл бұрын
If I understand you correctly, yes it should work fine!
@CodingWithUnity5 жыл бұрын
If I understand you correctly, yes it should work fine!
@Icey_Editss5 жыл бұрын
Yea sorry I thought this was roblox sorry
@TheNamesJT5 жыл бұрын
how do you make it so when you gain experience my image.fill amount fills up? having issues because experience is an int and the image fill amount is a float. I tried changing the values in the level script to floats but it broke some methods so changed it back to int....would appreciate the help man...
@CodingWithUnity5 жыл бұрын
convert it after you retrieve the int, and before you use it on your image.fill
@TheNamesJT5 жыл бұрын
@@CodingWithUnity not sure what you mean because im new to this whole c# and unity. Watched this video to learn....could you possibly provide some code? if its an easy fix? Umm so like; // this is how im accessing the level script public Level level; public float expConversion; level.experience = expConversion; expConversion = experienceBarImage.FillAmount; Update: nope that didn't work :(
@CodingWithUnity5 жыл бұрын
its hard to say exactly because im guessing on how your doing it and its been a few weeks now, also sorry about that, youtube only updates my of initial comments and not replies to comments, its frustrating. But try this public Level level; experienceBarImage.FillAmount = (float)level.experience; Im not sure if your trying to convert to a float or an INT, im assuming a float, but by using () around a variable type next to another variable type it essentially converts the second variable to be the same type as whatever is inside the (). So float num1 = 3.0; int num2 = (int)num1; would convert float num1 to an int thats accessable using num2. Or you can get rid of num2 alltogether and just use (int)num1 (Sorry the first reply wasnt very detailed its hard to assume how much the person knows that im replying too)
@TheNamesJT5 жыл бұрын
@@CodingWithUnity Thanks for trying, but decided to scrap the leveling system in my game as it seemed like to much of a hassle to implement and get the UI to work....appreciate the response at least.
@CodingWithUnity5 жыл бұрын
@@TheNamesJT ahh darn! I could probably put a quick UI together for you if you ever wanted to try it again. Just join my discord and shoot me a message!
@yahoruz4 жыл бұрын
this is also so dope :O
@CodingWithUnity4 жыл бұрын
thanks a lot
@stevedoll19824 жыл бұрын
It's a nice system... now for my problem... I am trying to pass a calculated XP (BaseXP * PlayerLevel) from the Enemy to the Player on death. But I cannot figure out how to pass the XP to the player. It's attaching the Level script to the Enemy and granting itself the XP. So I have a dead enemy with 15 XP. I'm sure it's something super basic I'm not thinking about.
@CodingWithUnity4 жыл бұрын
You just need a Public float XP; On the enemy script and then on your player script have the Public Level level; Then when you attack the enemy maybe pass a reference of the player to the enemy and then if the attack kills the enemy you can use that reference to the player to access the level so you can do player.level.AddExp(XP);
@stevedoll19824 жыл бұрын
@@CodingWithUnity Awesome. Thank you. Knew it was something like that. Basically what I did was what you suggested for public float XP, but I used PlayerLeveling (I already had a different Player script which I didn't want to mess around with too much), in Void Start I added playerLeveling = GameObject.Find("Player").GetComponent(); used the playerLeveling.level.AddExp(XP); and now that I know how to do that I can apply that method to a bunch of stuff I was thinking about. Thanks again!
@bistou4864 жыл бұрын
@@stevedoll1982 wouldn't it be better to store a variable in the inspector and drag the player in it so it already have the reference ? i heard that using .Find is not recommended for big project but people use it in tiny stuff and tutorial
@stevedoll19824 жыл бұрын
@@bistou486 I don't doubt it is. But I have no idea how to do that.
@bistou4864 жыл бұрын
@@stevedoll1982 erm i'm so bad at explaining things to others xD i'll try so instead of getting in start, at the top where you have your variables put one for the player like public PlayerLeveling levelSystem; and then save and you should get a new box in the inspector where you can drag the player gameObject from the hierarchy to that box and that will give you access to those scripts to write levelSystem.level.AddExp(Xp); not sure how you wrote your scripts, i guessed based on the things you said in earlier comments.
@JPOfAwesomeness4 жыл бұрын
Is there a link to the code?
@CodingWithUnity4 жыл бұрын
bitbucket.org/Sniffle6/beta-xp-system/src/master/ This isnt this videos code specifically but it has a form of the code in it. Ill try to get the actual code to this video uploaded here soon
@JPOfAwesomeness4 жыл бұрын
Coding With Unity thanks, me and my friend are making a game and my friend figured out how to make a leveling system based off of score, this video was very informative ethier way.
@NIVIS-s4d2 жыл бұрын
Thank you!
@Birch555 жыл бұрын
can u plzz start using github it would make life sooo much easier
@CodingWithUnity5 жыл бұрын
Yes ill start uploading the code
@Nazo2245 жыл бұрын
nice
@CodingWithUnity5 жыл бұрын
@Abel-uc9do4 жыл бұрын
1 22
@ELGATOGAMER3 жыл бұрын
A idea for a Level system using System.Collections; using System.Collections.Generic; using UnityEngine; public class PlayerLevel : MonoBehaviour { public int Level; public float CurrentExperience = 10; public int RequiredExperience = 25; public float Percent; public Fill fill; void Start () { //CombatEvents.OnEnemyDeath += EnemyToExperience; Level = 1; } void Update() { RequiredExperience = (Level * 25) * (10 * Level); if(CurrentExperience >= RequiredExperience) { Level = Level + 1; } Percent = CurrentExperience / RequiredExperience ; fill.Refill(Percent); } public void GrantExperience(int amount) { CurrentExperience += amount; } }