Unity Health and Damage System - Combat Tutorial (VRoid)
Vložit
- čas přidán 22. 08. 2024
- In this tutorial, you'll learn how to create health and damage system in Unity and VRoid Studio. This is part one of the new melee combat tutorial with VRoid characters. I'm very excited for this series!
Part Two Combat Tutorial: • The Complete Damage Po...
In this tutorial, you'll learn:
- How to create a health and attack attribute
- How to handle incoming damage and dealing damage to other units
- This tutorial is intended for absolute beginners in Unity programming
- We'll go over some basic concepts in programming
If this video helps you, don't forget to like and subscribe!
Download the project here:
github.com/ReF...
SOCIAL MEDIA:
Patreon: / reforgemode
Ko-Fi: ko-fi.com/refo...
Twitter: / feyhunt
Discord: / discord
GitHub: github.com/ReF...
#VRoid #Unity #Tutorial #VRoidStudio
If you have any questions, feel free to ask it in the comments below!
Is there any video, about activating damage script when damage animation is 70-90% done or something?
@@nikson12gg Yes, I did create a follow up to this video here: czcams.com/video/6MYmzIz7rE4/video.html
@@ReForgeMode Thanks
Glad to see, that you are better and back doing these videos.
You're pretty much the only one who does Vroid and Unity videos.
Thank you! Oh, surely not! I know some people who do it better than I do...
And I'm planning to catch up to them soon. I should be able to post more regularly now that my schedule is mostly clear 😄😄
Love the pacing! Quick to the point while still providing lots of extra information. Looking forward to more!
Thank you! Pacing is one of many issues I have with these videos. I will try to make it even snappier next time!
So glad that you went over all the basics! Most tutorials skip this sort of information but as a new developer this is very useful. I've subscribed :)
Thank you! Glad I could help!
@@ReForgeMode I have yet to see the other tutorial videos, but would a good alternative to this be to make separate attributes for the player and the enemies? So that you would have PlayerHP, PlayerAttack and EnemyHP, EnemyAttack for example. To me this sounds like it may be easier to work with, but I'm new to developing so I might be missing something. Curious what you think :)
Edit: I should probably add that I also wrote a simple Defense integer that substracts the Attack damage dealt by x Def amount
That is the beauty and major cause of headache in programming. There are so many ways to implement an idea, you have to choose which one is appropriate.
Separating player and enemy script is an option. But it has several downside:
- Harder to maintain. If in the future, you decided to add a new attributes, say Magic Defense, you will have to modify both of the scripts manually. If you accidentally name the attributes differently, like attack and atack in these two scripts, it will not be fun.
- What if: enemy attack each other? Let's say you implemented a mechanic where enemies can now attack each other. Maybe it's as simple as friendly fire between enemies, or maybe the enemies have multiple factions that can attack each other. How do you code enemies damaging each other? If you separate the script for enemies and player, you will have to check if the thing attacked is either a player or an enemy, and then use the appropriate GetComponent function, for every enemy damaging abilities.
- What if: multiplayer? Maybe it's a co-op with friendly fire, or a PvP content. How do you code player damaging other players? Same as before, you will need to check if the player damages an enemy or a fellow player in every single hit.
This won't be an issue if you make the attributes universal to the player and enemy. The attacker shouldn't need to care what it hits.
But there is a middle ground. Later on, you might want to implement something called Interface or Inheritance, so you can have a variation of the same script, but you don't need if statements to get the right GetComponent function. This basically solves all of the problems above.
And... I'm making that video right now!
Hopefully that answers your question!
@@ReForgeMode Ohh thank you so much for the detailed reply and insight, that makes sense! Really helps
Glad I could help! 😄😄
Love this, really opens things up for me, among others’ tutorials.
Eventually I want to include “slow” effect on damage, “damage over time” effect on damage, audio events on damage, and simple indication visuals/ animation events on damage.
But what I want to learn first is how to make either, my Heads Up Display’s crosshair, or my player camera and enemy’s raycast create damage hit.
Glad I can help you!
Crosshair is very easy. Just put an image in a Canvas and place it at the center of your screen. You're already on the right track to raycast the enemy and deal damage, just use Camera.ScreenToWorld and then Physics.Raycast.
Excited to see what you'll make next! 😄😄
bro thats a very well teached tutorial.
Oh, thank you! I am also working on Health Bar UI next. Could be a while to finish though hehe.
Very simple and useful tutorial thank you
Glad I could help! 😄😄
Underrated channel, you just got 1 sub bro!
Thank you! Glad I could help!
no rushing 10/10
No rushing here! All in due time 😄😄
Cant select my enemy as a object
Could you elaborate on this? You can select your enemy object by clicking on their name in the Hierarchy Window.
LESGOOOOOO
Yes! I'm so excited. Never made a tutorial series before. I hope I won't disappoint!
@@ReForgeMode Very informational video. Thanks for this quality content. Really appreciated!
Great Tutorial, I just had one question.
So I have a day night cycle animation in my project, and I want it so that my damage is different at day. How can I do this?
Interesting. Are you making a vampire game perhaps?
It'a quite simple. Create a float variable for day/night cycle, then when dealing damage, check if the time of day is day or night.
@@ReForgeMode Damn dude your guess was spot on.
I'll try your idea, and let you know.
Thanks a ton
How would I implement this to a first person shooting game, I want my projectile that I’m shooting to deal damage. And I want the monster to damage me when they collide with my player
Then you just need to call the damage function when the enemy collides with your character collider with OnColliderEnter. Same thing for bullets.
@Reforge Mode you make a great video. I have a question.
You use the damage tester with a key bind f11 f12 but this only makes effect if i use this keys. What about if i want to make damage with a bullet object.
I was thinking oncollider hit to activate the attribute manager functions?
If you can expand the combat tutorial that would be awesome. But anyways it really help me this video. Thank you
Thank you! You can use OnCollider other.GetComponent() to get the reference to your health script and call the damage function.
i cant add DamageTester script to DamageTester object because script class cannot be found
Hmmm... It's usually an error because of the class name is different than the script name or a typo. Try it and see if it works.
Im newer to learning unity how would I have it reset player position/destroy enemy when the health hits 0 I’m using the 2021 unity
Then I recommend browsing the official Unity Learn website to get the basics.
The short answer is to use transform.position to the player and using Destroy(gameobject) for the enemy.
@@ReForgeMode thank you I appreciate!
@@themop2890 No problem! Excited to see what you'll make in the future!
I am always tortured by making fighting collision. If we want to use fists to attack enermies, it seems not appropriate to use box collider or any other collider, because they will detect other things before detecting enermies. Have you used boxcast before? I wonder if I can use BoxCast to serve as collider
That's why you should specify which layerMask you're targeting in BoxCast. LayerMask allows you to only interract with specific things, like enemy colliders.
Of course, you need to first separate each object in your game into separate layers, like terrains, enemies, interactibles, etc.
I have one where the enemy is a dragon and I have a sword, as combat animation and I have a combo system, How do I do basic health and damage, like Heath is 500 and damage is 10 per strike/combo, easily, I've seen multiple tutorials and everyone's is so different I am confused.
I gotcha fam. Basically, you need to create a collision box to trigger the damage. I made a tutorial for it right here:
czcams.com/video/6MYmzIz7rE4/video.htmlsi=4n6X6jCvNlLQk0DI
I don't know why, but every time I press the button I select, the objects take much more damage than I assigned to them (I put 5 damage on the enemy but the ally receives more than 200 damage xD).
I think there may be a problem with multiple triggers. Make sure you're using Input.GetKeyDOWN instead of Input.GetKey. GetKeyDown will only register one button press, while GetKey will trigger every frame as long as the button is pressed. Let me know if this helps!
Also, if the enemy and player doesn't appear, it's because you should open the Scene in the Project Window first. Double click to open it. By default, Unity always loads in default empty scene.
It would be interesting if you explain a quest system like genshin impact.
We'll get there eventually, but we need to finish the combat system first since it's highly requested.
@@ReForgeMode That'll also be great haha thanks!
I did exactly as you said but the code error shows AttributesManager error (type or namespace not found)
The most likely cause is a typo somewhere. Make sure to name your scripts and class name carefully. Lowercase or uppercase letters matter.
Great tutorial although the "GetKeyDown" thing isnt working
The syntax is Input.GetKeyDown(KeyCode.F12). Have you double checked you didn't mistype the keys? Or perhaps press the wrong key?
No I did everything in the video
@@hamismad123 There are many reasons why the key doesn't get registered:
- Make sure you added the script as a component in the inspector and it's not disabled.
- Make sure that GetKey exist in the Update function
...and many more.
I need more details to help you out.
This works great for 1v1 combat, but how is this sysstem supposed to be implemented when there are multiple enemies present?
Well, with every collision from the player's weapon, it will deal damage to all enemies.
I don't get how you can say it's great only for 1v1. It's actually the opposite, good for multiple enemies, not so good for a 1v1 fight like in some fighting games.
@@ReForgeMode Maybe I'm confused, wouldn't you have to then hook up the attribute manager for each individual enemy present in the scene?
@@westkossuth No, that's not how it works. Let me explain.
So the way you program this is that when the OnHit collision happens, it will immediately try to get the enemy's attribute component and call in DealDamage function to it.
It doesn't matter how many enemies you hit. As long as there is contact with the collider, their function will be called.
The only thing you need to do to set up the enemy is to make sure they have that Attribute component.
Later on, you can also incorporate different types of Attributes component if there are some enemies that takes damage differently, like with Defense or special mechanics.
Let me know if you need more help! 😄😄
when we press F12 key the health reduces. what to write in if condition if we want to crick button from joystick??
It depends what joystick you use and what platform your game is running in. i recommend checking out Input System in Unity.
really good tutorial! Sadly nothing happens when health = 0. and why doesnt this work:
if (health = 0)
{
destroy (gameObject, life);
}?
Thanks! Destroy(gameObject, life) means that you're trying to destroy this specific gameObject with a time delay of "life" seconds.
It works, but there is a massive delay. What are you trying to do there?
If you want to have an object with multiple life count, instead add this inside the first if:
If(life > 0)
{
life--;
health = maxHealth;
}
else
{
Destroy(gameObject);
}
when I start and restart the game my health wont restart it just keeps the previous value
Hmmm... that is weird. Is this the health value in the inspector?
yes@@ReForgeMode
@@dogukantombul1045 Okay, is there any function in your script that sets the health manually? Something like health = 2?
Are you using ScriptableObject to store the value?
Without looking at your script, I can't tell what is wrong. You can join our discord server and share your script right there. Link is in the description.
do u think i could use this in vrchat
I'm not sure. This is a Unity tutorial. Is VRChat based on Unity?
ya but no it use udo sharp@@ReForgeMode
script pls
It's in the description. Just download the Github project.
i need your help in my game, how can i contact you?
Feel free to contact me through my email at reforgemode@gmail.com or PM me on Discord feyhunt