Unity3D Beginners - Add leveling to your game

  Рет қаралды 15,881

Andy W

Andy W

Күн бұрын

Пікірлер: 81
@danielsalyi7194
@danielsalyi7194 4 жыл бұрын
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!
@danielsalyi7194
@danielsalyi7194 4 жыл бұрын
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
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
awesome job! Thanks for posting :)
@ExileClips123
@ExileClips123 4 жыл бұрын
Thank you!
@KaganParlatan
@KaganParlatan 4 жыл бұрын
Thanks Daniel
@mikethegamedev
@mikethegamedev 4 жыл бұрын
...
@CodingWithUnity
@CodingWithUnity 3 жыл бұрын
New game! Take a shot each time I say "level up" and see if you can finish the video
@NiclasGleesborg0
@NiclasGleesborg0 5 жыл бұрын
Great video. It's a really good system.
@ashfid8987
@ashfid8987 5 жыл бұрын
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.
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
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
@ashfid8987
@ashfid8987 5 жыл бұрын
Coding With Unity shit, I am so excited for the inventory system haha. Scriptable Object is too good.
@kapkoder4009
@kapkoder4009 4 жыл бұрын
Thanks for this! Actions are also something pretty cool to take away from this video :)
@FyresGames
@FyresGames 3 жыл бұрын
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.
@CodingWithUnity
@CodingWithUnity 3 жыл бұрын
awesome!
@leventocakci
@leventocakci 2 жыл бұрын
how can i implement this to using database version
@Golem1988
@Golem1988 Жыл бұрын
How does one also display exp required to get the next level and how does one update that value on level up?
@TrevorPinkney
@TrevorPinkney 4 жыл бұрын
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
@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_3336
@scott_3336 4 жыл бұрын
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_md
@general_md 4 жыл бұрын
How do I put the values of the XP on a slider?
@sambell675
@sambell675 5 жыл бұрын
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? :)
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
Sure just make a public Level Attack; public level Strength; public level defence; and so on :)
@redbeardjunior
@redbeardjunior 4 жыл бұрын
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?
@simaanakbar9550
@simaanakbar9550 5 жыл бұрын
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
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
Did you get this working? Cant remember, pretty sure you asked on discord
@fantasymagic2108
@fantasymagic2108 4 жыл бұрын
The vendetta voice, thanks for the tutorial
@ExileClips123
@ExileClips123 4 жыл бұрын
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?
@ExileClips123
@ExileClips123 4 жыл бұрын
i know im just missing something basic, i always find it hard to use stuff from one script into another
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
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)
@ExileClips123
@ExileClips123 4 жыл бұрын
@@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 👍👍
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
@@ExileClips123 Awesome! Glad you got it working!
@satisfyingworld419
@satisfyingworld419 5 жыл бұрын
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
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
look into serializing classes, its pretty strait forward!
@DigitalPianoClassical
@DigitalPianoClassical 4 жыл бұрын
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?
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Thats kinda the next step, use this formula to populate a lookup table :)
@bistou486
@bistou486 4 жыл бұрын
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 ?
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
Linked you to it in discord!
@ExileClips123
@ExileClips123 4 жыл бұрын
@@CodingWithUnity can you link here? Also interested
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
@@ExileClips123 Just follow the brackeys HP bar tutorial and replace the HP values with the exp values
@ExileClips123
@ExileClips123 4 жыл бұрын
@@CodingWithUnity now that's smart, thanks xD
@Hazzardous2005
@Hazzardous2005 4 жыл бұрын
@@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_Editss
@Icey_Editss 5 жыл бұрын
So you know a cash on kill right? But what if instead of cash it’s exp will it still work
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
If I understand you correctly, yes it should work fine!
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
If I understand you correctly, yes it should work fine!
@Icey_Editss
@Icey_Editss 5 жыл бұрын
Yea sorry I thought this was roblox sorry
@TheNamesJT
@TheNamesJT 5 жыл бұрын
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...
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
convert it after you retrieve the int, and before you use it on your image.fill
@TheNamesJT
@TheNamesJT 5 жыл бұрын
@@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 :(
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
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)
@TheNamesJT
@TheNamesJT 5 жыл бұрын
@@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.
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
@@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!
@yahoruz
@yahoruz 4 жыл бұрын
this is also so dope :O
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
thanks a lot
@stevedoll1982
@stevedoll1982 4 жыл бұрын
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.
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
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);
@stevedoll1982
@stevedoll1982 4 жыл бұрын
@@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!
@bistou486
@bistou486 4 жыл бұрын
​@@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
@stevedoll1982
@stevedoll1982 4 жыл бұрын
@@bistou486 I don't doubt it is. But I have no idea how to do that.
@bistou486
@bistou486 4 жыл бұрын
@@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.
@JPOfAwesomeness
@JPOfAwesomeness 4 жыл бұрын
Is there a link to the code?
@CodingWithUnity
@CodingWithUnity 4 жыл бұрын
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
@JPOfAwesomeness
@JPOfAwesomeness 4 жыл бұрын
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-s4d
@NIVIS-s4d 2 жыл бұрын
Thank you!
@Birch55
@Birch55 5 жыл бұрын
can u plzz start using github it would make life sooo much easier
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
Yes ill start uploading the code
@Nazo224
@Nazo224 5 жыл бұрын
nice
@CodingWithUnity
@CodingWithUnity 5 жыл бұрын
@Abel-uc9do
@Abel-uc9do 4 жыл бұрын
1 22
@ELGATOGAMER
@ELGATOGAMER 3 жыл бұрын
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; } }
Unity3D - Level Scaling for beginners
13:56
Andy W
Рет қаралды 9 М.
Level Up with Experience Points in Unity
8:27
BMo
Рет қаралды 17 М.
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН
Support each other🤝
00:31
ISSEI / いっせい
Рет қаралды 81 МЛН
It works #beatbox #tiktok
00:34
BeatboxJCOP
Рет қаралды 41 МЛН
A Great Way To Setup POWERUPS In Your Unity Game
13:43
Terrain Terraforming in Unity - Mining With A Pickaxe
17:19
Freedom Coding
Рет қаралды 3,7 М.
Make A Game Like Pokemon in Unity | #36 - Experience / Level Up System
29:26
Game Dev Experiments
Рет қаралды 8 М.
Unity RPG Tutorial: Leveling and Stats
59:36
Iain McManus
Рет қаралды 6 М.
EXPERIENCE and LEVELING System | Unity Tutorial
5:03
Pogle
Рет қаралды 6 М.
How to LEVEL UP Player Status in Unity - Inspector [1/3]
20:14
Beaver Joe
Рет қаралды 4,8 М.
Turn-Based Combat in Unity
29:40
Brackeys
Рет қаралды 592 М.
Level Up! How to make a Scalable Level System: Unity Tutorial
21:43
Natty GameDev
Рет қаралды 30 М.
The Best Band 😅 #toshleh #viralshort
00:11
Toshleh
Рет қаралды 22 МЛН