🌐 Have you found the videos Helpful and Valuable? ❤️ Support on Patreon www.patreon.com/unitycodemonkey or get the Game Bundle unitycodemonkey.com/gamebundle.php
Hey bro i found the tutorial very useful but i was wondering how to instansiate the healthbar as a child of the player and the enemies And distroy it when the health =0
i know Im randomly asking but does anybody know a trick to log back into an instagram account?? I somehow lost my account password. I love any tricks you can offer me
@Coleman Gael thanks so much for your reply. I got to the site on google and I'm trying it out atm. Looks like it's gonna take a while so I will reply here later with my results.
@Coleman Gael DAMN IT ACTUALLY WORKED :O I just hacked my IG password within roughly 40 minutes by using the site. Just had to pay 15 bucks but for sure worth it :O Thank you so much you saved my ass !
You would get more views if you focus on explaining your code and take more time in it. You type really fast and are quite quick not very easy for beginners to learn stuff
Please add // also for coder like us we know only c++ c we don't know header file using unityengine; Using codemonkey; is header file code monkey user defined or inbuilt
Code Monkey just the while video in general, you don’t tell me what it does thoroughly and you type too fast for me to follow what the code does, don’t get me wrong it’s good but it would be better for beginners to maybe slow down but of course it’s ur channel so u do whatever u want but it’d be a bit easier for beginners to follow if u were slower.
Same here why Debug.Log is being used? Maybe you have explained them in previous videos but I am new to your channel. If you have already explained why you've added certain code in earlier videos you could refer to them while you speak and post a link for em in the current videos description! Great job with the tutorial! Thanks a lot!
The tutorials were excellent. But as a learner, I had hard time to follow or to catch up with your speed. I have done coding and I understand coding but as I am new to unity, I didn't get why did you do that step. I felt like I am supposed to know the unity already and should have game coding experience in unity. If there would have been an explanation for lines of code, you wrote, about why are we doing this then it would have been easy for me to follow. I understand that I lagg in unity undersatnding. ANyways, thanks for the tutorials.
If you have any specific question please to post it and I'll do my best to answer. The videos do assume a certain background in Unity and C#, I'm planning on doing some more beginner focused videos soon. In the newer videos I have made an effort to try and explain every single line of code before testing so check those out and see if they're easier to follow. Thanks!
10:18 "making a Prefab out of HeathBar object" Just a bit of an explanation how to do it in "Unity 2018.3" (and newer): there is no need to create an empty Prefab through menu Create->Prefab (basically there is no such option now). You just grab an object HealthBar and drag-and-drop it to the Asset/Prefab folder.
Loved the video... Although it is a speedy walk through, I do like how clean and thought out the code is... In my humble beginner opinion... :) Great stuff Sir...
Sorry I’m asking so many questions, but for some reason your CMDebug is not working. It won’t show up. I tried making it so that if you press a key it would take damage and heal, but that also didn’t work. Help needed
This is perfect and a very simple and tidy tutorial, I like that you introduce every watcher to a number of possibly new things compared to how a lot of tutorials are out there. They usually make scripts that is a one of, which works for a simple game having a couple mechanics. This is more usuable if you want to build on top of it. And the decoupling is very nice to see. It does take more from the usual tutorial watcher, but thats good imo. I used Unity UI which im not sure how works when it comes to optimization, you say its quite costly, and I might have to change this later on. I thought they were priced almost the same as a normal game object in the scene. The other thing about UI is that you need more code to support it, hiding it etc if you dont want it in view at all times. I added a UI anchor script for the HPbar script and just passed the transform from the game object that spawns it into the SetupBar(HPsystem, Transform) and set the "follow" to that transform.
Glad you liked the video! If you do it in the UI but you want it following a unit in the world then you'll have to do viewpoint calculations on every single frame. It's not an issue if you just have one character but if you have hundreds like in an RTS then all those calculations will start to hit your performance.
Bro, your videos are pretty amazing. I am new to the UNITY and your videos literally can explain me clearly. Your are the best bro. I am expecting the new videos from u.
@@CodeMonkeyUnity the whole vid bud. pausing doesnt explain why you do what you do. Simply copying isnt learning. Not for total beginners anyway.This channel is more aimed at people with some sort of knowledge for C#. Not bashing your channel just saying you'll be talking a foreign language to newbies
@@chrisdk5665 Yeah the videos all require some base of knowledge depending on the topic, some are for complete beginners and some require some basic knowledge of Unity and C#. This was my very first video and I've improved massively at explaining everything I do, check out my more recent videos.
Man please i really need your Help, my console didnt Show me the Health like you in 2:39 idk what im doing wrong :/ please it would be awesome if u can help me pls
No, ive learned a lot about how to programm in unity, save data, about how to controll the cinemachine and post processing, solving the problems and i want to keep using unity because i think is a great tool and if u have a problem maybe it will took days, weeks but at the end you will understeand how to do it.
This health bar is mean to be placed on top of a Unit that exists in the game world so it makes sense to use a normal Game Object instead of the UI. If I made it as a UI bar I would have to do a world position to ui position calculation every single frame which if you have multiple bars at once could become costly. If you only have a single unit that you wish to display the health, like just the player, then it would make sense to have a single UI Health Bar.
@@CodeMonkeyUnityThank you for this information can you please make a tutorial about profiling and how to track performance and optimization that would be very helpful
Code Monkey, this is RUclips channel, what everyone has been searching for all the time. If there is someone, who is not subscribed Code Monkey yet, I recommend! Here you can check some cool tips for coding, what everyone wants to do. Good job Code Monkey, continue on this way! 💪
@@CodeMonkeyUnity Im not sure if who are in your channel really need a complete C# core course (IMHO). But some core concepts, from who come from WindowsForms programming , as a example, Its not quite clear how to use events (or the difference between events and UnityEvent, seens the same to me) in a game. I already watched a lot of your videos and this caracteristc (events) in your code is very strong and I liked. So, when you think about this choice in your code, you can do a video =)
I have a question about this system and applying it to other objects (Enemies, etc.), but does there NEED to be a gamehandler for EVERY object? I feel like this point wasn't explained too well
Nope, that's just for testing. You would simply do new HealthSystem(); in your Player/Enemy class to create a health system. By the way I have since remade this system and made it into a proper free asset unitycodemonkey.com/video.php?v=tf8NhUgORQg
What exactly do you mean by random spawn? In this video the spawning of the enemies is pretty basic, they spawn on playerPosition + randomDirection * random(50f, 100f);
The health doesnt show up, Nothings comes in my console beside code from my other scripts public class HealthSystem { public int health; public HealthSystem(int health) { this.health = health; } public int GetHealth() { return health; } } using UnityEngine; public class GameManager : MonoBehaviour { // Start is called before the first frame update void Start() { HealthSystem healthSystem = new HealthSystem(100); Debug.Log("Health: " + healthSystem.GetHealth()); }
Once I've reached the certain point of knowledge of Unity and coding I will watch entire channel in way of searching for the answers which will eventually comes xD
Hi, how do you get the health bar to follow your player? i know you dont make healthbar the child of player, but dont see where you copy position values.
In the end I did make it a child of the player, just didn't record that part. That's the simple way to do but you can also keep it a global object and just make a script to follow a Transform on Update.
help!!! i followed all he steps but when i clicked on play this error poped up ( error CS0123: No overload for 'HealthSystem_OnHealthChanged' matches delegate 'EventHandler' )
How could I make a health system but the health bar is a diagram of a plane and when it gets damaged to a certain extent it changes Color from green to yellow to red?
the script I typed from the sample video could not be attached to the game object, for a reason the script don't inherit a native class that can manage a script
Why did you create an extra class only for the health? Why not put it into the healthbar script where you won't have any trouble instantiating a new object of class Health and then assign it to the healthbar script? Wouldn't it be better to have the code of a specific part at that specific part?
SOLID principle. One class should only do one thing. It's more scalable, for example, because you can use it for enemies, or other entities which doesn't need a healthbar. Reuseability.
Code Monkey I am a newcomer to the world of coding and game development. So I have many doubts more than my imagination . So I request you to do tutorial somewhat in a explanation manner
Hello,around the time 8:30 I get a problem which is NullRefrenceException: Object reference not set to an instance of an object Health bar May someone please help
The healthbar doesn't work for me. I had to change x position from "5" to "0.5" in the bar and "-5" to "-0.5" on the spritrbar in order to get it to not shift away. Hope that helps you.
When the x position is at "5", the barsprite shifts left when the scale is lowered. I don't know why changing it to "0.5" on the x position fixes the shifting and it works for me.
I really going to follow u 😀 I was intrested in coding and stuff in unity but i tried brackey channel tutorial but the cod was not working and i stoped opening unity i will now try it again with ur tutorial 😎
I created a PlayerCollision script that makes it sense whenever the player touches an item. When it does this, the player would take damage. How would I get the healthSystem (HealthSystem healthSystem = new HealthSystem(100);) into the PlayerCollison? Thanks
Anyone else have a problem with the healthbar being way off camera? It's working perfectly other then it doesnt follow the player. I've rewatched the video countless times, but I guess the part that shows how to lock the HP bar above its object isn't in the video.
The class doesnt care where the damage came from, you just need to call the damage function wherever you want to deal damage. So in that case wherever you are handling monster damage call the function.
The only videos that have a specific order are in Playlists. So check those out and for all the other videos you can pretty much just pick a topic you're interested in and watch it.
The Health System is great and functions perfectly. I'm a bit new to programming though and am curious. How can I modify the health system to account for multiple enemy and player types having different max healths?
Assets\2DPlatformerCharacterController\Scripts\GameHandler.cs(2,7): error CS0246: The type or namespace name 'CodeMonkey' could not be found (are you missing a using directive or an assembly reference?) is there a solution?
Where are you seeing that? The 2D platformer isn't used in this video. If you want a health system I've since polished this system in a newer video unitycodemonkey.com/video.php?v=tf8NhUgORQg
GameHandler is just attached to a Game Object, nothing special about it. The HealthSystem doesn't extend MonoBehaviour so it's not attached ot anything.
Got a question about how did you architected your player. I've heard that its a good practice to make a player with scripts in a modular way, where you will have 1 script for example dedicated for the player movement and another script for example the health system. I've seen that in the proyect files you use all of that in the PlayerHandler, so my question is. What's your opinion on using this moduled version, is it just better to have 1 script in the player that manages everything or making a moduled version. And also if you want to upgrade for example a stat like max health or damage, how could this be done? Thanks 😀
It really depends on the situation, depends on how complex is the game, how complex is the project you're making, etc. Sometimes I make the health a field on the player, other times I make it a separate component, both options can be valid. I made a modular character controller here ruclips.net/video/mJRc9kLxFSk/видео.html And I did a new version of this health system here ruclips.net/video/tf8NhUgORQg/видео.html
I am having a problem attaching the health bar to an enemy. When I attach the script to an enemy the health bar Instantiates in the same spot it was originally created and is not attached to the enemy. Is there something that I am missing?
The Health Bar should be instantiated as a Child of your Enemy game object so it follows along. I only noticed that after I stopped recording so that part isn't shown in the video. Just change the Instantiate code Transform healthBar = Instantiate(pfHealthBar, enemyTransform); Vector3 healthBarLocalPosition = new Vector3(0, 10); healthBar.localPosition = healthBarLocalPosition;
I don’t understand how you moved the anchor point of the bar sprite, I’ve watched multiple tutorials of yours that explain that but it never works for me
Those are made using my utilities, perhaps you have your UI set up in a different way, but you can use any buttons you want like the built-in Unity UI buttons, all it matters is how it interacts with the HealthSystem
Im having some trouble with the health bar, when i test it, instead of scaling down on the X, it scales up on the Y, and its when printing the characters health, could you please help me?, it would be greatly appreciated.
Do you see any errors in the console? What size is your camera? Pause the video, chances are the button was created but outside the camera. Or you're using a 3D Camera.
In this video you made your own health bar from scratch, but I'm using one from the Energy Bar Toolkit. It has an Energy Bar script which reveals Value Current, Value Min, and Value Max in the Inspector. How do I feed these values from your HealthSystem into the Energy Bar script so that the custom Energy Bar displays the health correctly? Thank you for your response.
This video is quite good but i got a question to ask as i use your script. How do you change your maximum health. As like in game if you wear armor your maximum health will increase
Hi, great tutorial, I'm having problems with the sprites size. I tried in another computer and worked fine, but in my computer I'm having this problem. Tried reinstalling Unity, then the entire system, older versions of unity... nothing works. Any suggestions?
I have tried several resources from the "project files" on unitycodemonkey.com, but in no case the resource works when i try to use same texture sprite or material. I bought one of the udemy course and everything works ... so I think it is as intended.
I was able to instantiate the pfHeathBar as a child of my Player but the Health Bar rotates with my Player, how can i fix this? Great Tutorial, best channel so far, i feel like i can create my entire game just from watching your videos! Thank you so much for the great content.
Liked the video, but you seemed to go over some things to fast. I understand it is hard to cover everything, but your videos might be better if you explained why you are doing things more then just showing the steps.
Not sure what you mean, the event isn't static so the event only belongs to a single instance Also I made an updated version of this system here unitycodemonkey.com/video.php?v=tf8NhUgORQg
@@CodeMonkeyUnity I'll check that out now, but I guess my question means if I put this same script on enemy prefabs, will it not conflict with the player?
That would be a very needlessly complex answer to this problem, you would essentially create a new canvas for every single health bar you wanted to create. And the canvas is really meant for UI elements so if something is meant to live in world space then you should make it in world space. It would work but I wouldn't use that approach even if you just wanted one health bar.
ok it worked but I have a problem with my prefab because when I press play its really small and it isnt on the camera because its on a canvas and it doesnt stay with the player it just stays still
The HealthBar is built using the SpriteRenderer so it should be placed in the World and not in the Canvas. Is everything else small or just the bar? If it's everything then check your camera OrthographicSize.
Hi , your videos are great i am looking forward to use unity. Do you think my pc can handle unity? My specs are= intel pentium g3220 6gb ram 500g hd Intel hd graphics ( I also have a nvidia quadro fx 1800 but im not using it ) +im total newbee in programming
I would of loved to do some of the Debugging tools like the damage and heal buttons but im in VR. Any suggestions? Im kinda following without the debugging and its causeing me a bit of time cause when i have an issue it doesnt show immediately. Also the health bar is on a world space canvas so im having to adapt the code of course
You need some way for the unit who is attacking to grab a reference to the Health System of the enemy taking damage. Then just call the damage function.
followed nicely BUT! Where is hell did Game handler come from, is it an empty object placed on the scene with the game handler script attached? there was no explaining this!
Hi! Nice video, thanks a lot. But could u please explain me why unity devs don’t like getters / setters. You method GetHealth() and private field could be just replaced with Health {get;}
I don't know about other Unity developers but for me I'm not a fan of Properties, they look like Variables but act like Functions, I'd rather keep my code as explicit as possible
@@CodeMonkeyUnity Wow that was fast Thank you! The idea to move it along the character was my thought as well, but how can I access the variables you defined in the start funktion?
hello, hey, on 5:58 im having trouble to understand why this works. Im new to unity and Im not getting what is Bar for, I mean, its an object with just a "transform" so its basically a position, why does the scale of the Bar affects on the BarSprite?? And why the shift of barsprite to the right and bar to the left? Can someone please help, Im using this method but I wanna know how it works!!
The Bar is just an empty GameObject that serves as the pivot for the inner BarSprite which is the GameObject that contains the actual sprite. So by shifting the BarSprite to the right you are making the Bar work as a pivot on the left. Then with the Bar working as a left pivot you can change the scale to easily modify the BarSprite size going from left to right. If you left the BarSprite on localPosition 0,0 then by changing the scale of the Bar it would scale the BarSprite from both sides. Hope that helps!
Why Cant I call the Damage function or the heal function in a different script? I have a collider that is working but i want to add that it does damage but it wont let me or i dont know how??
@@no.sixtyseven8195 You create the HealthSystem on whatever unit/player script you have. If you want to use it as a component I made an updated version of this system here unitycodemonkey.com/video.php?v=tf8NhUgORQg
I’m having a problem with setting the healthsystem = new HealthSystem(max health)… unity says you can’t create new through a mono behavior and should use add component or scriptable object it says in error console.. I try to add a variable gameobject to trygethealthsystem out of it and it’s just really hard to get it working.. I got it working earlier and even got fire to damage my character… then I tried adding scriptable objects for playerhealth and I screwed it all up 😅
The HealthSystem in this video is a regular C# class, it does not extend MonoBehaviour By the way I have an updated version of this system here unitycodemonkey.com/video.php?v=tf8NhUgORQg
@@CodeMonkeyUnity yes I have that healthSystem .. Sorry, I thought I was commenting on that video.. In the healthsystem component (I meant), it uses MonoBehaviour and has the healthSystem = new HealthSystem(100) and thats what's giving me a error. I'm going to try to reimport everything again and maybe I messed with something
@@CodeMonkeyUnity Nevermind I figured it out!!! I also implemented scriptable objects and combined the healthbar and healthsystem together so that I can drop it on anything I want to have health and a healthbar, along with the healthsystem component to just get the healthsystem for other scripts... I erased everything on healthsystem component and put it on to the regualr healthsystem. Listen ok... it wasn't working so i made it work LMAO
I don't see where you explain how you add the health to the enemies...the enemies just have the health bar outta the blue. I want the player and enemies to use the same system.
You instantiate the HealthSystem wherever you need it. So on your Enemy Class you would have a member variable and instantiate it with new HealthSystem(healthAmount);
It's giving me an error. It says "transform.localScale assign attempt for 'Bar' is not valid. Input localScale is { Infinity, 1.000000, 0.000000 }. UnityEngine.Transform:set_localScale (UnityEngine.Vector3)". Can you help?
To deal damage you just call the function, from where you do that depends on what you're trying to do, there's a multitude of ways to do it. You can have a projectile with Physics and call Damage on collision, or maybe you want an instant shot and you call it based on a Raycast, or maybe it's based on a Animation System.
🌐 Have you found the videos Helpful and Valuable?
❤️ Support on Patreon www.patreon.com/unitycodemonkey or get the Game Bundle unitycodemonkey.com/gamebundle.php
Hey bro i found the tutorial very useful but i was wondering how to instansiate the healthbar as a child of the player and the enemies
And distroy it when the health =0
i know Im randomly asking but does anybody know a trick to log back into an instagram account??
I somehow lost my account password. I love any tricks you can offer me
@Brooks Aries Instablaster ;)
@Coleman Gael thanks so much for your reply. I got to the site on google and I'm trying it out atm.
Looks like it's gonna take a while so I will reply here later with my results.
@Coleman Gael DAMN IT ACTUALLY WORKED :O I just hacked my IG password within roughly 40 minutes by using the site.
Just had to pay 15 bucks but for sure worth it :O
Thank you so much you saved my ass !
You would get more views if you focus on explaining your code and take more time in it.
You type really fast and are quite quick not very easy for beginners to learn stuff
What specific part of the video did you find hard to understand?
Please add // also for coder like us we know only c++ c we don't know header file using unityengine;
Using codemonkey; is header file code monkey user defined or inbuilt
Code Monkey just the while video in general, you don’t tell me what it does thoroughly and you type too fast for me to follow what the code does, don’t get me wrong it’s good but it would be better for beginners to maybe slow down but of course it’s ur channel so u do whatever u want but it’d be a bit easier for beginners to follow if u were slower.
Same here why Debug.Log is being used? Maybe you have explained them in previous videos but I am new to your channel. If you have already explained why you've added certain code in earlier videos you could refer to them while you speak and post a link for em in the current videos description! Great job with the tutorial! Thanks a lot!
Debug.Log is used to display some information in the console and in this case it displays the health
The tutorials were excellent. But as a learner, I had hard time to follow or to catch up with your speed. I have done coding and I understand coding but as I am new to unity, I didn't get why did you do that step. I felt like I am supposed to know the unity already and should have game coding experience in unity. If there would have been an explanation for lines of code, you wrote, about why are we doing this then it would have been easy for me to follow. I understand that I lagg in unity undersatnding. ANyways, thanks for the tutorials.
If you have any specific question please to post it and I'll do my best to answer. The videos do assume a certain background in Unity and C#, I'm planning on doing some more beginner focused videos soon.
In the newer videos I have made an effort to try and explain every single line of code before testing so check those out and see if they're easier to follow.
Thanks!
Thanks :)
Getting a code monkey ad on code monkey's video
Code monkey: Stonks
thats like they are paying and getting it at the same time
I got an ad about your video on another video xd
Hope you liked what you saw!
sure do!
Lol
10:18 "making a Prefab out of HeathBar object"
Just a bit of an explanation how to do it in "Unity 2018.3" (and newer): there is no need to create an empty Prefab through menu Create->Prefab (basically there is no such option now). You just grab an object HealthBar and drag-and-drop it to the Asset/Prefab folder.
Can you help me?
Loved the video... Although it is a speedy walk through, I do like how clean and thought out the code is... In my humble beginner opinion... :) Great stuff Sir...
Glad you liked it!
Sorry I’m asking so many questions, but for some reason your CMDebug is not working. It won’t show up. I tried making it so that if you press a key it would take damage and heal, but that also didn’t work. Help needed
amazing tutorials! the speed and explanations are awesome, u did a good job! for a unity noob like me it's just the right stuff to learn on. keep up!
Glad you like the videos!
This is perfect and a very simple and tidy tutorial, I like that you introduce every watcher to a number of possibly new things compared to how a lot of tutorials are out there. They usually make scripts that is a one of, which works for a simple game having a couple mechanics. This is more usuable if you want to build on top of it. And the decoupling is very nice to see. It does take more from the usual tutorial watcher, but thats good imo.
I used Unity UI which im not sure how works when it comes to optimization, you say its quite costly, and I might have to change this later on. I thought they were priced almost the same as a normal game object in the scene. The other thing about UI is that you need more code to support it, hiding it etc if you dont want it in view at all times.
I added a UI anchor script for the HPbar script and just passed the transform from the game object that spawns it into the SetupBar(HPsystem, Transform) and set the "follow" to that transform.
Glad you liked the video!
If you do it in the UI but you want it following a unit in the world then you'll have to do viewpoint calculations on every single frame.
It's not an issue if you just have one character but if you have hundreds like in an RTS then all those calculations will start to hit your performance.
This is where everything began.
It's been a long journey! Thanks for being here!
Bro, your videos are pretty amazing. I am new to the UNITY and your videos literally can explain me clearly.
Your are the best bro.
I am expecting the new videos from u.
More videos coming soon! Thanks!
Im trying to make a game during my spring break. This vid helped me out a lot. Thanks bro I appreciate it.
That's awesome! Best of luck!
Stay away if you’re absolute beginner, you will learn nothing but how fast this guy can type
You can pause the video at any time if you want to follow along. Where did you get lost?
@@CodeMonkeyUnity the whole vid bud. pausing doesnt explain why you do what you do. Simply copying isnt learning. Not for total beginners anyway.This channel is more aimed at people with some sort of knowledge for C#. Not bashing your channel just saying you'll be talking a foreign language to newbies
@@chrisdk5665 Yeah the videos all require some base of knowledge depending on the topic, some are for complete beginners and some require some basic knowledge of Unity and C#.
This was my very first video and I've improved massively at explaining everything I do, check out my more recent videos.
Please fix! why the healthBar.Setup(healthSystem) won't work on start method, it only works if it's in the update method
What do you mean by doesnt work? Do you have a NullReferenceException?
@@CodeMonkeyUnity Assets\Scripts\GameHandler.cs(13,19): error CS0122: 'HealthBar.Setup(HealthSystem)' is inaccessible due to its protection level
@@danielmurillo748 Sounds like you made Setup() private but are trying to access it from another class
Just to say thank you, without you I wouldn't have made it. Thanks
I'm glad the videos have helped you! Best of luck with your games!
Man please i really need your Help, my console didnt Show me the Health like you in 2:39 idk what im doing wrong :/ please it would be awesome if u can help me pls
same thing is happenning to me
im guessing you have given up already
No, ive learned a lot about how to programm in unity, save data, about how to controll the cinemachine and post processing, solving the problems and i want to keep using unity because i think is a great tool and if u have a problem maybe it will took days, weeks but at the end you will understeand how to do it.
@@aka_bearz im having the same issue :( please help
@@mkgames4539 So u cant see results in the console? Be sure u are attaching your Script to a GameObject
very simple and effective code
U should type slow and explain code to the viewers so bigginers like us can also understand
What part couldn't you understand? Take you time and pause as much as you need, this is your learning journey, not a race
Why you did't use UI instead of GameObject for the health bar ?
is this better for performance ?
Thank you for the tutorials and good job
This health bar is mean to be placed on top of a Unit that exists in the game world so it makes sense to use a normal Game Object instead of the UI.
If I made it as a UI bar I would have to do a world position to ui position calculation every single frame which if you have multiple bars at once could become costly.
If you only have a single unit that you wish to display the health, like just the player, then it would make sense to have a single UI Health Bar.
@@CodeMonkeyUnityThank you for this information
can you please make a tutorial about profiling and how to track performance and optimization that would be very helpful
Thank you for making the game so easy to understand
Code Monkey, this is RUclips channel, what everyone has been searching for all the time. If there is someone, who is not subscribed Code Monkey yet, I recommend! Here you can check some cool tips for coding, what everyone wants to do. Good job Code Monkey, continue on this way! 💪
Hey man, congrats for your videos!! I saw you use a lot of events. Can you do a video explaining how it works? Keep the good job!
Yeah I'd love to do a complete C# course one day and explain all the core concepts.
@@CodeMonkeyUnity Im not sure if who are in your channel really need a complete C# core course (IMHO). But some core concepts, from who come from WindowsForms programming , as a example, Its not quite clear how to use events (or the difference between events and UnityEvent, seens the same to me) in a game. I already watched a lot of your videos and this caracteristc (events) in your code is very strong and I liked. So, when you think about this choice in your code, you can do a video =)
Your videos really help alot of people 👍❤️🙏
I have a question about this system and applying it to other objects (Enemies, etc.), but does there NEED to be a gamehandler for EVERY object? I feel like this point wasn't explained too well
Nope, that's just for testing. You would simply do new HealthSystem(); in your Player/Enemy class to create a health system.
By the way I have since remade this system and made it into a proper free asset unitycodemonkey.com/video.php?v=tf8NhUgORQg
I have the codeMonkey utilities installed and set up yet the buttons will not show up. What should I do?
Pretty Cool. It would be nice if you make a random spawn tutorial
What exactly do you mean by random spawn? In this video the spawning of the enemies is pretty basic, they spawn on playerPosition + randomDirection * random(50f, 100f);
The health doesnt show up, Nothings comes in my console beside code from my other scripts
public class HealthSystem
{
public int health;
public HealthSystem(int health)
{
this.health = health;
}
public int GetHealth()
{
return health;
}
}
using UnityEngine;
public class GameManager : MonoBehaviour
{
// Start is called before the first frame update
void Start()
{
HealthSystem healthSystem = new HealthSystem(100);
Debug.Log("Health: " + healthSystem.GetHealth());
}
Hey dude,
i love your RUclips video and i want to see more videos from your channel
Thanks!
Once I've reached the certain point of knowledge of Unity and coding I will watch entire channel in way of searching for the answers which will eventually comes xD
How can I make the player die when health = 0
1
Hi, how do you get the health bar to follow your player? i know you dont make healthbar the child of player, but dont see where you copy position values.
In the end I did make it a child of the player, just didn't record that part.
That's the simple way to do but you can also keep it a global object and just make a script to follow a Transform on Update.
help!!! i followed all he steps but when i clicked on play this error poped up ( error CS0123: No overload for 'HealthSystem_OnHealthChanged' matches delegate 'EventHandler' )
What is the function signature for HealthSystem_OnHealthChanged?
It needs to be (object, EventArgs)
How could I make a health system but the health bar is a diagram of a plane and when it gets damaged to a certain extent it changes Color from green to yellow to red?
You can use a Mask to get a health bar of any shape docs.unity3d.com/Packages/com.unity.ugui@1.0/manual/script-Mask.html
Then change the image.color
the script I typed from the sample video could not be attached to the game object, for a reason the script don't inherit a native class that can manage a script
Make sure you extend MonoBehaviour
Why did you create an extra class only for the health? Why not put it into the healthbar script where you won't have any trouble instantiating a new object of class Health and then assign it to the healthbar script?
Wouldn't it be better to have the code of a specific part at that specific part?
SOLID principle. One class should only do one thing. It's more scalable, for example, because you can use it for enemies, or other entities which doesn't need a healthbar. Reuseability.
Yes sir please do somewhat slowly so that we could understand. This will surely help in your channel growing
What specific part of the video did you find hard to understand?
Code Monkey I am a newcomer to the world of coding and game development. So I have many doubts more than my imagination . So I request you to do tutorial somewhat in a explanation manner
Hello,around the time 8:30 I get a problem which is NullRefrenceException: Object reference not set to an instance of an object Health bar
May someone please help
did you forget to add the script to the health bar?
Can you show how you got that blood effect on the ground?
Yeah I'm planning on making a video about that particle system sometime in the future, cheers!
The healthbar doesn't work for me. I had to change x position from "5" to "0.5" in the bar and "-5" to "-0.5" on the spritrbar in order to get it to not shift away. Hope that helps you.
What didn't work? The local size depends on the parent size, and the parent size will depend on how big you want it to be and the size of your camera
When the x position is at "5", the barsprite shifts left when the scale is lowered. I don't know why changing it to "0.5" on the x position fixes the shifting and it works for me.
I really going to follow u 😀
I was intrested in coding and stuff in unity but i tried brackey channel tutorial but the cod was not working and i stoped opening unity i will now try it again with ur tutorial 😎
Great Channel, immediately subscribed! Please go on with 2D unity games, I would LOVE to see also Android game Apps. Thank you
Thanks!
Code Monkey how many years of experience do you have with C# programming and Unity engine?
I've been working with Unity and C# for 5 years.
I started doing game development in Flash in 2008 then transitioned to Unity in 2013.
Want it a better idea to declare the health variables as float in the first place?
Depends on what values you want for your health, if you need decimal points yes use float, if you just need whole numbers use int
I created a PlayerCollision script that makes it sense whenever the player touches an item. When it does this, the player would take damage. How would I get the healthSystem (HealthSystem healthSystem = new HealthSystem(100);) into the PlayerCollison? Thanks
Anyone else have a problem with the healthbar being way off camera? It's working perfectly other then it doesnt follow the player. I've rewatched the video countless times, but I guess the part that shows how to lock the HP bar above its object isn't in the video.
Either constantly update the healthBar.position to match the player.position or make it a child of the player.
@@CodeMonkeyUnity Thanks! I subscribed to your channel by the way I can't wait to watch your other videos!
Where i have to put HealthSystem script?
Anywhere in your project files, its just a simple class.
@@CodeMonkeyUnity Thank you
If I want to change from the damage button to a monster, do damage instead. how i can do this. Thank you
The class doesnt care where the damage came from, you just need to call the damage function wherever you want to deal damage.
So in that case wherever you are handling monster damage call the function.
@@CodeMonkeyUnity How to call function. Do you have a sample?
What's the software u r using
I'm using Unity and Visual Studio
How do I code a special move for an RPG character
My console says nothink
Wow, AMAZING TUTORIAL.....WHAT COMPILER YOU ARE USING?
It's being compiled by Unity using the normal compiler.
For learn how to crete a game i must start with this video? Or i can take causal video and learn to it without move in order
The only videos that have a specific order are in Playlists.
So check those out and for all the other videos you can pretty much just pick a topic you're interested in and watch it.
The Health System is great and functions perfectly. I'm a bit new to programming though and am curious. How can I modify the health system to account for multiple enemy and player types having different max healths?
Each enemy or player has its own instance of a HealthSystem, so each of those can have different maximum values
Pls tell me what is GameHandler.
It's just the script used for testing the Health System, simple MonoBehaviour added to a gameObject.
@@CodeMonkeyUnity thx i had an error now it works again xd
Thx
Assets\2DPlatformerCharacterController\Scripts\GameHandler.cs(2,7): error CS0246: The type or namespace name 'CodeMonkey' could not be found (are you missing a using directive or an assembly reference?)
is there a solution?
Where are you seeing that? The 2D platformer isn't used in this video.
If you want a health system I've since polished this system in a newer video unitycodemonkey.com/video.php?v=tf8NhUgORQg
For some reason when I do the transform.Find to get the health percentage it doesn't work and I don't know why and there are no errors in the console.
Where are you putting Gamehandler and HealthSystem?
GameHandler is just attached to a Game Object, nothing special about it. The HealthSystem doesn't extend MonoBehaviour so it's not attached ot anything.
Got a question about how did you architected your player.
I've heard that its a good practice to make a player with scripts in a modular way, where you will have 1 script for example dedicated for the player movement and another script for example the health system. I've seen that in the proyect files you use all of that in the PlayerHandler, so my question is. What's your opinion on using this moduled version, is it just better to have 1 script in the player that manages everything or making a moduled version.
And also if you want to upgrade for example a stat like max health or damage, how could this be done?
Thanks 😀
It really depends on the situation, depends on how complex is the game, how complex is the project you're making, etc. Sometimes I make the health a field on the player, other times I make it a separate component, both options can be valid.
I made a modular character controller here ruclips.net/video/mJRc9kLxFSk/видео.html
And I did a new version of this health system here ruclips.net/video/tf8NhUgORQg/видео.html
@@CodeMonkeyUnityokay thanks a lot! 😊
I am having a problem attaching the health bar to an enemy. When I attach the script to an enemy the health bar Instantiates in the same spot it was originally created and is not attached to the enemy. Is there something that I am missing?
The Health Bar should be instantiated as a Child of your Enemy game object so it follows along. I only noticed that after I stopped recording so that part isn't shown in the video.
Just change the Instantiate code
Transform healthBar = Instantiate(pfHealthBar, enemyTransform);
Vector3 healthBarLocalPosition = new Vector3(0, 10);
healthBar.localPosition = healthBarLocalPosition;
@@CodeMonkeyUnity Thank you for this. It is much appreciated.
everything work properly until finish except button ui codemonkey didnt appear in the last time when it in the prefab. can you help me ?
Thank you for posting this video, could you please tell me where I attach the two scripts to? Would HealthSystem be attached to the Player?
HealthSystem is a simple class so you can create it wherever you want like inside your Player class.
Thanks,very useful!
I don’t understand how you moved the anchor point of the bar sprite, I’ve watched multiple tutorials of yours that explain that but it never works for me
You move the inner object to the right of the parent origin. Then when you scale the parent it will act like a pivot on the child.
Im kinda stuck at 3:48. I can't get the buttons to show at all :(.
But hey, great content and great tutorials!
Those are made using my utilities, perhaps you have your UI set up in a different way, but you can use any buttons you want like the built-in Unity UI buttons, all it matters is how it interacts with the HealthSystem
Im having some trouble with the health bar, when i test it, instead of scaling down on the X, it scales up on the Y, and its when printing the characters health, could you please help me?, it would be greatly appreciated.
Check how you're modifying the localScale and make sure you're changing localScale.x and leave localScale.y as it is or set it to 1.
cool vid sauce thanks for all the sweet sauce tips
UI Buttom from Utilities does not work... I downloaded and added. Then, I used debug in the same was as on the video. The button does not appear.
Do you see any errors in the console? What size is your camera? Pause the video, chances are the button was created but outside the camera. Or you're using a 3D Camera.
@@CodeMonkeyUnity no errors in the console. I did some debugging, and the code is executing. Yes, i use a 3d camera. It does not work for 3D?
@@CodeMonkeyUnity How can I fix it for 3d camera?
You are just the best!!
This is a great video, but how do I make it if health = 0 it turns my animator off?
if (health == 0) animator.enabled = false;
Or a better way, listen to the OnDied event and disable the animator
In this video you made your own health bar from scratch, but I'm using one from the Energy Bar Toolkit. It has an Energy Bar script which reveals Value Current, Value Min, and Value Max in the Inspector. How do I feed these values from your HealthSystem into the Energy Bar script so that the custom Energy Bar displays the health correctly? Thank you for your response.
IT works on 3d game ?
This video is quite good but i got a question to ask as i use your script. How do you change your maximum health. As like in game if you wear armor your maximum health will increase
You would make another function to modify the healthMax that is set in the constructor.
Hi, great tutorial, I'm having problems with the sprites size. I tried in another computer and worked fine, but in my computer I'm having this problem. Tried reinstalling Unity, then the entire system, older versions of unity... nothing works. Any suggestions?
I have tried several resources from the "project files" on unitycodemonkey.com, but in no case the resource works when i try to use same texture sprite or material. I bought one of the udemy course and everything works ... so I think it is as intended.
my health bar is not apearing above the player and i dont know how to fix that :/
I was able to instantiate the pfHeathBar as a child of my Player but the Health Bar rotates with my Player, how can i fix this? Great Tutorial, best channel so far, i feel like i can create my entire game just from watching your videos! Thank you so much for the great content.
Make a script that resets the rotation on Update(); and add it to the bar.
transform.eulerAngles = Vector3.zero;
@@CodeMonkeyUnity Oh, Thank You So Much!!
Liked the video, but you seemed to go over some things to fast. I understand it is hard to cover everything, but your videos might be better if you explained why you are doing things more then just showing the steps.
What's to stop a heal event for the player from healing enemies too with events broadcasting like that?
Not sure what you mean, the event isn't static so the event only belongs to a single instance
Also I made an updated version of this system here unitycodemonkey.com/video.php?v=tf8NhUgORQg
@@CodeMonkeyUnity I'll check that out now, but I guess my question means if I put this same script on enemy prefabs, will it not conflict with the player?
@@ImTheMrFoxman Each HealthSystem instance is separate. They will only share the healthsystem if you intentionally reuse the same object
@@CodeMonkeyUnity ah! Okay, perfect. Thank you for the replies.
Why did you choose to use a sprite as HB instead of a world space canvas attached to the player with a filled image?
That would be a very needlessly complex answer to this problem, you would essentially create a new canvas for every single health bar you wanted to create. And the canvas is really meant for UI elements so if something is meant to live in world space then you should make it in world space.
It would work but I wouldn't use that approach even if you just wanted one health bar.
Sir it is terrain or a cube and if a terrain how you added these grass textures
The background is simply a SpriteRenderer with a grass texture. Cheers!
ok it worked but I have a problem with my prefab because when I press play its really small and it isnt on the camera because its on a canvas and it doesnt stay with the player it just stays still
The HealthBar is built using the SpriteRenderer so it should be placed in the World and not in the Canvas.
Is everything else small or just the bar? If it's everything then check your camera OrthographicSize.
Hi , your videos are great i am looking forward to use unity.
Do you think my pc can handle unity?
My specs are=
intel pentium g3220
6gb ram
500g hd
Intel hd graphics
( I also have a nvidia quadro fx 1800 but im not using it )
+im total newbee in programming
Sure probably, it's free so just download and try it out.
I would of loved to do some of the Debugging tools like the damage and heal buttons but im in VR. Any suggestions? Im kinda following without the debugging and its causeing me a bit of time cause when i have an issue it doesnt show immediately. Also the health bar is on a world space canvas so im having to adapt the code of course
why did u remove the inheritance between HealthSystem and MonoBehavior
To keep the code clean and as decoupled as possible. If it doesn't need MonoBehaviour there's no reason to add unnecessary dependencies.
someone can Help me how can I decrease the player healthbar because the player took damage there is part that i could'nt understand
there is a posibility por us to get some of the assets from your videos?
The assets are included in the project files for this video.
How do i apply this to when it hits an enemy, it loses 10 hp?
You need some way for the unit who is attacking to grab a reference to the Health System of the enemy taking damage. Then just call the damage function.
I don't understand how you used the console to check the code. If it's in another video, please let me know which.
You mean the log messages? It's just Debug.Log();
@@CodeMonkeyUnity i cant find it either. following what you did, it does not show where to put the GameHandler script
@@TheAmishUpload The GameHandler is simply attached to a Game Object just like any Unity component.
followed nicely BUT! Where is hell did Game handler come from, is it an empty object placed on the scene with the game handler script attached? there was no explaining this!
Yes the GameHandler script is just attached to an empty game object so you can use the Start as an entry point to the code.
Thank you mate , I might come across dumb, but it threw me right off, thanks for the tut
Hi! Nice video, thanks a lot. But could u please explain me why unity devs don’t like getters / setters. You method GetHealth() and private field could be just replaced with Health {get;}
I don't know about other Unity developers but for me I'm not a fan of Properties, they look like Variables but act like Functions, I'd rather keep my code as explicit as possible
How do I make the Healthbar move along with my Char?
Either make it a child of the player or make a script to constantly update its position
@@CodeMonkeyUnity Wow that was fast Thank you! The idea to move it along the character was my thought as well, but how can I access the variables you defined in the start funktion?
hello, hey, on 5:58 im having trouble to understand why this works. Im new to unity and Im not getting what is Bar for, I mean, its an object with just a "transform" so its basically a position, why does the scale of the Bar affects on the BarSprite?? And why the shift of barsprite to the right and bar to the left? Can someone please help, Im using this method but I wanna know how it works!!
The Bar is just an empty GameObject that serves as the pivot for the inner BarSprite which is the GameObject that contains the actual sprite.
So by shifting the BarSprite to the right you are making the Bar work as a pivot on the left.
Then with the Bar working as a left pivot you can change the scale to easily modify the BarSprite size going from left to right.
If you left the BarSprite on localPosition 0,0 then by changing the scale of the Bar it would scale the BarSprite from both sides.
Hope that helps!
@UCFK6NCbuCIVzA6Yj1G_ZqCg thanks!! I understand better now
Can we use mask and slider UI for the health system also will it be effecting the performance ?
Why Cant I call the Damage function or the heal function in a different script? I have a collider that is working but i want to add that it does damage but it wont let me or i dont know how??
public class CubeKiller : MonoBehaviour
{
private void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Destroy")
Destroy(other.gameObject);
//Need to damage the health system here!
}
}
You need to do something like other.gameObject.GetComponent().Damage();
@@CodeMonkeyUnity how can i do that when the health system script cant be put on an object?
@@no.sixtyseven8195 You create the HealthSystem on whatever unit/player script you have.
If you want to use it as a component I made an updated version of this system here unitycodemonkey.com/video.php?v=tf8NhUgORQg
I’m having a problem with setting the healthsystem = new HealthSystem(max health)… unity says you can’t create new through a mono behavior and should use add component or scriptable object it says in error console.. I try to add a variable gameobject to trygethealthsystem out of it and it’s just really hard to get it working.. I got it working earlier and even got fire to damage my character… then I tried adding scriptable objects for playerhealth and I screwed it all up 😅
The HealthSystem in this video is a regular C# class, it does not extend MonoBehaviour
By the way I have an updated version of this system here unitycodemonkey.com/video.php?v=tf8NhUgORQg
@@CodeMonkeyUnity yes I have that healthSystem .. Sorry, I thought I was commenting on that video.. In the healthsystem component (I meant), it uses MonoBehaviour and has the healthSystem = new HealthSystem(100) and thats what's giving me a error. I'm going to try to reimport everything again and maybe I messed with something
@@CodeMonkeyUnity I’m gunna let you know if I get it
@@CodeMonkeyUnity it says HealthSystem is missing the class attribute ExtensionOfNativeClass
@@CodeMonkeyUnity Nevermind I figured it out!!! I also implemented scriptable objects and combined the healthbar and healthsystem together so that I can drop it on anything I want to have health and a healthbar, along with the healthsystem component to just get the healthsystem for other scripts... I erased everything on healthsystem component and put it on to the regualr healthsystem. Listen ok... it wasn't working so i made it work LMAO
also i try to create on my own and i get CMDebug not exist... Is it because of the Using CodeMonkey?
Yes CMDebug is a helper class part of the CodeMonkey Utilities which you can download from the website.
i see but what if codemonkey goes down in the future i will also lose the cmdebug right ?
or if you download it it does not matter ?i mean if it needed to work required the codemonkey website
The package contains the source for the utilities, there's no network connection.
I don't see where you explain how you add the health to the enemies...the enemies just have the health bar outta the blue. I want the player and enemies to use the same system.
You instantiate the HealthSystem wherever you need it.
So on your Enemy Class you would have a member variable and instantiate it with new HealthSystem(healthAmount);
Can you upload the code files that you wrote on this video in your website? I know it's an old video but I'm stuck almost in the end. Thanks!
How do i make it so that when the player connects with an object that has a tag, for example "bullet" they get damage dealt
You can use the Physics system to listen to collisions ruclips.net/video/Bc9lmHjqLZc/видео.html
It's giving me an error. It says "transform.localScale assign attempt for 'Bar' is not valid. Input localScale is { Infinity, 1.000000, 0.000000 }. UnityEngine.Transform:set_localScale (UnityEngine.Vector3)". Can you help?
Are you dividing by 0? You cannot assign something to infinity scale
@@CodeMonkeyUnity Yeah maybe. Can you tell me how to fix it?
@@loldude8238 You have to look at how you're calculating the health value
@@CodeMonkeyUnity Thanks, I rewached the video and tried it again. It works. Thank you
this is confuseing can please show us from the start idk what did u add those to scripts to and where
What start? The basics of Unity? unitycodemonkey.com/video.php?v=E6A4WvsDeLE
I'm very confused. How do you add that health bar to other objects such as enemies. And how do you trigger the damage from other enemies?
To deal damage you just call the function, from where you do that depends on what you're trying to do, there's a multitude of ways to do it.
You can have a projectile with Physics and call Damage on collision, or maybe you want an instant shot and you call it based on a Raycast, or maybe it's based on a Animation System.