Unity3D Beginners - Add leveling to your game
HTML-код
- Опубликовано: 29 авг 2019
- In this video we will be using the converted code to formula from the last video to create a Level Up system to be used in a game!
First Video: • Unity3D - Level Scalin...
If you like this channel, or just Unity in general, consider joining my Discord at: / discord
********************************************************************
♥ Subscribe: bit.ly/2FRWgOi
● Facebook: bit.ly/2DhBC6I
● Twitter: / indie_unleashed
● Support me with Brave: brave.com/cod828
********************************************************************
● Make a Grid with a 1 dimensional array: bit.ly/2sRTpfL
● How to make Snake in Unity Tiny: bit.ly/2UmJHhe
● Climbing System Show off: bit.ly/2FPurq1
********************************************************************
● OSRS EXP Formula: oldschool.runescape.wiki/w/Ex...
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!
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
awesome job! Thanks for posting :)
Thank you!
Thanks Daniel
...
New game! Take a shot each time I say "level up" and see if you can finish the video
Thanks for this! Actions are also something pretty cool to take away from this video :)
Great video. It's a really good system.
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.
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
Coding With Unity shit, I am so excited for the inventory system haha. Scriptable Object is too good.
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.
awesome!
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.
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();
}
}
}
@@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 ☺️
this is also so dope :O
thanks a lot
The vendetta voice, thanks for the tutorial
Thank you!
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? :)
Sure just make a
public Level Attack;
public level Strength;
public level defence;
and so on :)
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?
Thats kinda the next step, use this formula to populate a lookup table :)
How does one also display exp required to get the next level and how does one update that value on level up?
How do I put the values of the XP on a slider?
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
Did you get this working? Cant remember, pretty sure you asked on discord
how do u do if u just did a task then give 100 exp
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?
how can i implement this to using database version
nice
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
look into serializing classes, its pretty strait forward!
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?
i know im just missing something basic, i always find it hard to use stuff from one script into another
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)
@@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 👍👍
@@Skaar01 Awesome! Glad you got it working!
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...
convert it after you retrieve the int, and before you use it on your image.fill
@@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 :(
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)
@@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.
@@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!
So you know a cash on kill right? But what if instead of cash it’s exp will it still work
If I understand you correctly, yes it should work fine!
If I understand you correctly, yes it should work fine!
Yea sorry I thought this was roblox sorry
can u plzz start using github it would make life sooo much easier
Yes ill start uploading the code
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 ?
Linked you to it in discord!
@@CodingWithUnity can you link here? Also interested
@@Skaar01 Just follow the brackeys HP bar tutorial and replace the HP values with the exp values
@@CodingWithUnity now that's smart, thanks xD
@@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.
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.
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);
@@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!
@@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
@@bistou486 I don't doubt it is. But I have no idea how to do that.
@@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.
Is there a link to the code?
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
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.
1 22
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;
}
}