Easy 2D Melee Combo System in Unity: Tutorial
HTML-код
- Опубликовано: 10 июл 2020
- Ever wanted an Easy solution at coding a 2D Melee Combo System inside the Unity Game Engine?
Correctly coding it can be frustrating: the majority of solutions you'll think of or find on the internet will be either overly complicated or not even work.
No worries! WIth this tutorial you'll be able to code your own 2d melee combo system in unity with:
- No Timers needed
- No Coroutines needed
- Just few lines of code
SPECIAL THANKS TO "GAMES BY SAUL" for helping me think about the logic of combo systems :)
Games by Saul RUclips Channel: / @gamesbysaul
----------
CHOSENPLAY SOCIAL LINKS:
----------
Discord: / discord
Facebook: / chosenplaystudio
Twitter: / chosen_play
IF YOU WANT TO SUPPORT US:
Patreon: / chosenplay Наука
UPGRADED VERSION OF THIS TUTORIAL TO BE FOUND IN HERE: ruclips.net/video/xr8UPU_X7A8/видео.html
I just wanted to thank you for this video! I'm not done to applying it in my project yet, but it's working! I was going crazy to make a damn chain of punches!
Very efficient, thanks! ^^
I'm very glad you found this useful! I hope I'll be able to make other valuable content in the future :)
Chardi gracias por el link al tuto me a ayudado :D
Informative & Concise! Keep em’ coming buddy!
Thank you, really glad you enjoyed it! Yeah I'll do more as soon as I have something to talk about that I think might be of use :)
I was watching your first video on this, and saw the second part in the comment section. This was far easier, and it makes total sense. I didn't even know I could attach scripts to animation states. Thank you so much!
This was an absolute diamond in the rough. It seems obvious now that you have explained it, but there aren't many other videos that discuss handling transitions, certainly not this well. Thank you!
I'm glad it was of help! ;) Soon I'll be posting an upgraded version of this were I'll show all the process of coding it from scratch. It'll be improved of course. Probably 2-3 videos from now :)
@@chosenplay6896 That's awesome! Keep up the great work.
This was simple, straightforward and dope! I used this as the basis for the combo system in my game. In less than 15 minutes you solved a problem I spent years working on! Dope bro, Dope!
Man, thank you very much! i had a little problems recreating this with my project but with the help of your answers in the comments and the Input video you made it worked just find!!! In some point i would love to make a version of this tutorial in spanish to share your method :D
I'm glad this helped you! :)
Also be sure to check the channel out cause some time ago I published an upgraded and WAY SIMPLER version of this combo system that you might find even more appealing :)
Wow, after looking up so many videos this is by far the easiest method. Thanks so much!!
Just amazing, since last year this kind of combo programming is making easy our developer life in game. But is not all programmers that know how to explaing it to us, so, you did an amazing work here and I just want to Thank you for this. Thank you so much.
I'm glad you found it useful :)
Clean, Easy, and Eloquent. Best setup and tutorial for combo attacks out there! Thank you for sharing the knowledge. You've got a subscriber here! :)
I'm glad this was of some help to you! :)
I was trying a kinda similar logic but would never get the coding done so easily if it wasn't for you, mister. Thank you!
Really glad you found this useful :) Also.. did you check the new upograded version of this? Might make your life even easier :)
Sorry for not checking this out sooner! I'm glad I was able to help (somehow) - next time I need to do 2D combat I'll definitely be using this ;D
No problem Dude. I hope it'll hep you and yeah, I'm really glad you helped out myself :)
@@chosenplay6896 :D
Thank you. Im new to programming(unity) and want to make 2d platformer, this helps alot.. defintely subscribing :) keep them coming!
This just makes it look easy! Thanks a lot!
You're welomce! Glad it helped :)
dude, you are a fucking beast.
I'm currently taking a IT course on game design but I really want to integrate my animation skills into it so I'm pouring a lot of effort on combo systems more than most students. This attack->transition-attack system is genius!
Thank you and love from the Philippines!
Can confirm. Went crazy trying to use timers.
so simple yet so so good thanks!
You're welcome. Glad you found that useful :)
Damn. Didn't know i could add scripts to animations. Thanks for that my friend.
You're welcome :)
Legendary. Thank you, good sir
This Helped A Lot Buddy Thanks Alot.
you're welcome :)
I've had a problem programming combo system today. While I won't use the animation scripts system you do (I'll stick to that good old Coroutines in transition states), thank you for the general idea.
Looks nice and simple. Good solution.
Btw, for your example you could have used the same trigger for every attack of the combo
You're actually right! Thanks :)
Thank u u are a great man saved a lot of coding work
Thank you for having an new input lesson this simple, im working on my first microgame and ive been trying to figure this out for a really long time the new system is very convoluted but flexible, and i am a low level kind of person so all these abstractions confuse the shit outta me.
as soon as I've done with assets creation I'll pubolish a full little serie that'll also contain an upgrade to this :)
@@chosenplay6896 nice! thanks look forward to checking it out and trying it out of course!
Easy, very simple, and its working thanks
Glad to have been of help :)
nice tut :D, love u
Thanks. There will also be an upgrade to this system as soon as I finish working on assets and start the serie I'm working on :)
Wow great video
congragulation for channel
Almost empty atm.. but thanks :)
I will try it, looks promising.
I'll soon be publishing an update to this :)
Interesting implementation, great video! Did you consider using animation events to set time windows where you can receive combo inputs? If so, did you find any downside?
I tried to do that before coming up with this method. I found it to be overly complex and convoluted and also not working a lot of times. Mainly due to animation freezing up and game breaking (mainly due to button mashing).
@@chosenplay6896 That's some valuable information. Thanks for your fast answer!
I found that problem too, and that's why I´m here... LOL
Hey There! A Superb Tutorial Thanks very much. Just a question, Does the Attack() Function supposed to be called anywhere?
That depends... Usually not, only as an event when the player press a button.. but it could also bne useful to be able and call it from elsewhere during cutscenes.
please do the new input system tutorial you talked about in the video man... It's so hard to keep seeing bad tutorials online and every time they're different... That's a weird topic that I've seen nobody explaining right... Thanks for this tutorial though, it really helped me refine my project :)
Done: ruclips.net/video/kgZbkbMkbIY/видео.html :) I hope I managed to explain it well I did my best ^^
My game is gonna be great for sure
I hope it will! Let us know :D
Thhanks mate :D
You're welcome :)
you are the best❤🎉
Ottimo video ;)
Grazie :)
is it realy only for 2D or can i use it in a 3D game also? and thanks for the sharing of ur great work my friend :)
Thank you for this video! Great explanation! This might be a dumb question but... what about is you want the player to walk-run? Do I replace Idle for Walk-Run state?
What do you mean "walk-run"? From attack directly to run? from walk to run? :)
@@chosenplay6896 I meant when you are walking or running then you want to melee attack.
@@miguelvelasquez6152 Yeah it's just the same.. you just got to connect the animation to the walkor the run one.. but that will not even be necessary when I?ll do the upgrade on the combo system in the future :)
@@chosenplay6896 Alright, thnk you sir. Ill be up for next videos.
so in short, what we have to do is that we just have to let the attack animation end after each button input, therefore no timing would be required. and if we press the input again mid animation, the attack will chain. did i get that right? im new and still trying to learn
Thank you man great video but can you make another video about parry system please
I'll add that to the "to do" list :)
Can you do the same Tutorial on Jumping attacks with combo?
thanks!
You're welcome :)
That's actually quite interesting approach. But is there a downside that we should aware of when apply the combo without the timer rather than using timer ?
If I understand your point correctly you'd like to know when to use this method or when to use a timer for a combo. I'd say the answer is, as it is for a lot of different things related to coding: "it depends on your needs".
For basic combo attacks I see absolutly no need to use timers most of the times BUT perhaps you want to have a "delayed" attack that can only be done if the button is pressed some seconds after the first attack.. in that case you would probably want to use a timer.
Did this help? If I didn't understand you point correctly please feel free to explain it better :)
@@chosenplay6896 Nah, i got it. Thanks for the explanation ;)
does this script spawn in the attack box that would allow the player to do damage to the enemies in a game?
Nice BRO
thanks :)
Really good tutorial, tho idk if its just my Audio setup but it was really quiet
Might very well be my setup.. I'll try to make it better for the next videos as soon as all the materials I'm preparing for them are ready :)
Transition!! Is the transition is emty ? Or idle state
In the soon to be released new tutorial with the upgraded version of this I'll try and show every single passage :)
Could you show how we should write the Attack function if we want to use to old input system? It's not quite working for me
Perhaps this might help: ruclips.net/video/kgZbkbMkbIY/видео.html :)
@@chosenplay6896 This totaly helped me thankS!!!!!
hello im a beginner game dev
how do I deal with different animation sizes, the idle size works fine but when it comes to attack it doesn't look right, my character just blinks and do the attack animation because of the size.
any help would be appreciated thanks
in the new tutorial I'm working on (upgraded combo system) I'll try and show how to correclty import an animation.
now the attack doesn't work at all, there are no errors or warnings and i did everything exactly like you, any idea why this is?
EDIT: also what i want is the first attack only being played once and the second and third attack repeating instead of all 3
It's also work on 3d too right?
Where can i get that sprite
hello, does those the transition animation state contain any animations?
If you're talking about the finite state machine they are contained inside animation states in Unity animator.
@@chosenplay6896 No i meant "transition1" and "transition2"
got it! Thank you so very much i really appreciate it
@@dennisdanso4160 Ah ok, I got what you meant I think. Yeah, they have animations in. The transitioning animation from attack to idle.
please do tutorial on how to catch androp enemy during melee combat u got my big support
Yo, what do you mean? Like a grabbing attack were the player literally grab the enemy and throw it away?
@@chosenplay6896 yes exactly
@@salahounissi1794 I'll see what I can come up with when I get some free time :)
@@salahounissi1794 Yo, would you mind contacting me on our discord channel? (you can find the link in the video description) I'd like to get a better understanding on your needs for this tutorial you asked for so I can be sure to get the right points in :)
hello friend, I'm trying to make a sword comb for my 2D character, but I'm still drawing the animation, you know where I find a good reference, because my game is vector
Other games.. play a lot, watch a lot, watch gameplay.. if you can screen record the games as you play them, cut the videos so you only see the animation you want and turn them to gif.. then you can see it frame by frame in many programs.
please make a full game
Your tutorial is very easy
I'm currently working on assets for a full tutorial serie on basic game creation :)
thank you can you make a video on how to cut the animation so it doesnt look wonky
I don't really get what you mean with this.. could you elaborate a little bit? Your animations "shakes?" when inside unity? Are they fine when you're drawing them in the art program?
@@chosenplay6896 thank you for replying fast i found that i messed up body rotations thanks
I would like to make my character transform into different shapes before jumping or running is there a way to make this possible
Well... if you're talking animatino wise you need to create the transform animation and then fire it before everything inside the game editor.. but from the question I guess you don't really know how Unity works as a game engine.. so may I suggest you take a course or something to learn how unity works first of all? :) I do not have such a course sadly.. but I can give suggestions about good ones if you want.
During transition one, when I press the button it doesn't lead me to attack two, I don't know why... Maybe the transition one and two are empty , I wonder if it has to have an animation
I'm starting like.. right now to work on the tutorial for the upgraded version of the combo system.. I hope to publish it soon :) This time I'll show every single passage on screen to avoid such problems.
@@chosenplay6896
Thank you very much for your work! I wait for the video. I am a new subscriber.
@@jorgeponce5147 You're welcome! I hope the channel's even tho small will be of use to you :)
P.S. was just making trials for the new system.. it's so easy to set up it's scary... if everything goes well it'll be on the channel by tomorrow evening or the day after I hope :)
@@chosenplay6896 Espectacular! esperando para verlo! Gracias...
So the attack transition is basically just a few frames from the end of the attack animation
Yes. Usually it's the motion that brings the character from the end of the attack back to Idle. LIke, just to make an example,, sheating the sword, or lowering the fighting stance.
why do you even call the input manager function after you write instance.inputReceived=false; ?
will that work on a startrun run and stoprun?
if to use what i just watch?
What is inside the transition animation
If you search the channel you'll find an upgraded version of this tutorial (should also be the link in the pinned comment of this video you've commented under) and in that video you'll see everything.. ofc also the transition animation.
Hi. Can you please show us each of your animations individually? You say that you have already created all animations but it will really help if I was able to each of them including the transitions then I could see how they all piece together
It's just as I said at the beginning of the video: an animation is made up of many frames. There are the ones that are "charging" the attack that are usually called "anticipation frames" then there is the attack itself and after that there are some frames were the character usually brings the weapon back to idle position.
Those last frames are the onest to use :)
Whats is the name of the first game ?
I get spammed with this error, think you can help me?
NullReferenceException: Object reference not set to an instance of an object
IdleBehavior.OnStateUpdate (UnityEngine.Animator animator, UnityEngine.AnimatorStateInfo stateInfo, System.Int32 layerIndex) (at Assets/IdleBehavior.cs:16)
I definitly can't without access to your code. But that said.. at the moment I'm not recording videos because I have a very important freelancing job that I need to finish and I do not ahve the time for tutorials.. but as soon as I'll be free the next video on the channel is going to be an upgraded version of this combo system. I'll also record it "from start to finish" with no cutting or particular editings so that such errors will be very hard to get :)
@@chosenplay6896 Maybe you could upload your project if you have it left and i can see my self what i did wrong
did you solve it? I get the same error
@@carmen-andreeabajanaru3319 same error
@@perssontm1628 same error
Does this method work for a 3D game or am gonna have to make adjustments
Not 100% sure cause I do not do 3d.. but since it's just a matter of linking animations I do not see why it shouldn't work ^^''
Good night, would it be possible to make the code available? I'm starting at Unity and I couldn't make it work like yours. I would like to analyze your example. thanks
I don't even have that code anymore but perhaps I might consider sharing the new one for the upgraded version that'll be released as soon as I can.. I'll consider it.
@@chosenplay6896 Cool, I would appreciate it very much.
@@guilhermelopesboroni6038 You can already find the upgraded version of the combo system on the channel.. it was published yesterday.
I decided not to put the code on but mainly because it's just so easy and with so few lines I considered it to be useless.. well you will see as soon as you check the video out :)
In the new version tho I showed everything with almost no cuts so it should be easy to set everything up :)
@@chosenplay6896 cool, thank you very much
holy crap, i already have shit tons of click ++ in my code. Any chance you can teach us how to make a simple combo system as well like up up down then use this skill (like street fighter)
Well.. at the moment I'm working on creating assets (most of them will be free on itch) for a complete tutorial/course serie on game dev in unity... I don't think this specific request'll be part of it BUT I think that would not be that hard to implement.. It's probably just a matter of creating custom input pattern (expecially in Unity new Input system it can be done quite easely) and then adding transitions :)
@@chosenplay6896 id love to see a tutorial on the new input system
please.. what is the name of the video games at the beginning, thanks for all
One is Dead Cells and the other is Guacameleeee
Tried it, doesn't look like it worked, can you recommend any videos or tutorials that might help with the button mashing problem?
Nope sorry.. I came up with this kinda on my own I've never seen a video for it myself...
@@chosenplay6896 I get ya. thanks all the same, and good video
Good tutorial but when i click nothing happens (by the way i am using old input Input.GetButtonDown("Fire1")) there is no spelling mistakes
If you want you can send me an unity package and I'll check it out when I can. The only other time this happened tho it was because they forgot to activate the "canreceiveinput" bool before trying it out :)
this is happening to me too, i activate the bool and if i press the fire key very very fast it works, or changing buttonDown to GetButton, but this is not correct becouse you can hold the button for ever and keep attacking, do you find any solution? thanks
Yes do the new input system tutorial please... :
I'm currently working on assets for a complete "let's create" serie of videos that'll also have the new Input system :)
@@chosenplay6896 Can't wait!!
Se puede para 3d??
Don't you need to set the canReceiveInput to true from the IdleBehavior script?
//override public void OnStateEnter(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
//{
// cm.canReceiveInput = true;
//}
At least, my implementation doesn't work unless I do that.
Since canReceiveInput is never initialized as true, you cannot check inputReceived, and the whole process never starts.
Yeah this was my first tutorial, I wasn't confident enough with my english and as such I recorded the audio and the video separatedly.. that made for all sort of issues when I did put it togheter and some things were mistakenly left out.
As you'll see tho I've changed way of doing these tutorials and I know do them directly from scratch showing every single step of the process. Soon enough (probably 3 videos from now) there'll be an upgraded version of the combho system tutorial that'll be published and every single step will be shown :)
THANKS! This totaly worked for me! :D
where are you calling the Attack Method? i dont get it :(
I do not "call it" because it's an event that works with the new input system. BUT if you want to use it with the old input system you just call it in the update :)
@@chosenplay6896 ahh now i understand. But i have 1 question: what exactly are you doing In Transition 1 2 and 3? are they only playing the same end-animation as AttackOne AttackTwo and AttackThree? or are they playing nothing? im really confused how the "inside" of these 3 Animations look in the "Animation"-Component (Not Animator)
nevermind i found out by myself im stupid :D
@@cratuss2661 Naaa you are not... after all you found it out by yourself didn't you? :D And even if you didn't.. it would probably be more a matter of me not managing to explain it well then you beeing tupid :)
Do you need to attach any of the scripts to the player?
If you do it the same way I did... everything but the finitestate scripts are on the player. But of course you can also put them on a random object, create a reference to the player and use it that way... you can always do stuff inmany different ways while coding :)
doesnt work? the behavior scripts dont recognise inputRecieved. CS1061
edit: spelling error
still deosnt work though
I'm starting right now to work on the upgraded version of this.. Will show every single passage on screen this time and hopefully it will solve problems some people are having :)
It won't work when I'm clicking, but it will play the first attack animation as soon as I play the scene. help
First of all... Relax :D Second: hit me up on discord and I'll see what I can do to help.
As a third: as soon as I have some free time there'll be a new tutorial with the new improved version of this coming up.. only this time I'll show every single step with almost no editing so that errors such as these will be hard to come upon :)
@@chosenplay6896 yes man lol, i am relax tho :(
Hello mate,
in my character, I'm using rig animations and there is some bugs. I will send you a video on discord server can you have a look ?
You can send it to me on discord in a private chat if you want and I'll definitly take a look at it. You should know tho that I've never used tig animations up until now.. always go with frame by frame art.. so don't be mad if I can't help.
Also... if you want you can also send a project package via mail no p0roblem so I can look at it directly inside unity and report back :)
@@chosenplay6896 hmm thats a better idea :)
I'll send you via email
@@chosenplay6896 btw what is your gmail :S
@@MertAliBarlas Know what? You're right: I can be an idiot at times :P it's "chosenplaystudio@gmail.com". I'll look at it as soon as I can :)
@@chosenplay6896 Hello again :D
I solved Luca. I was an idiot :/
how well does this work with 3d?
No idea, I don't do 3d :( You can try and let everyone know ^^
I did the tutorial, it didn't give any error, but it doesn't call the attack animations.
Did you set the "canreceiveinput" boolean to true?
@@chosenplay6896 On inspector yes, now if i click on trigger in animator call the animation, but not when i click the button if i set on code not happens
Hi can you help me? It doesn't work on me? everytime i press I nothing happens? i follow all the codes exactly and the animator diagram and exit time all but nothing happens?
im wondering when will be my pressed key (I) activated on the idlebehaviour? In the combat manager the attack method is not on update like in the video?
public class CombatManager : MonoBehaviour
{
public static CombatManager instance;
public bool canRecieveInput;
public bool inputRecieved;
void Awake()
{
instance = this;
}
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
}
public void Attack()
{
if (Input.GetKeyDown(KeyCode.I))
{
if (canRecieveInput)
{
inputRecieved = true;
canRecieveInput = false;
}
else
{
return;
}
}
}
public void InputManager()
{
if (!canRecieveInput)
{
canRecieveInput = true;
}
else
{
canRecieveInput = false;
}
}
}
---------------idlebehaviour-----------
override public void OnStateUpdate(Animator animator, AnimatorStateInfo stateInfo, int layerIndex)
{
if (CombatManager.instance.inputRecieved)
{
Debug.Log("test");
animator.SetTrigger("AttackOne");
CombatManager.instance.InputManager();
CombatManager.instance.inputRecieved = false;
}
}
i tried to debug it but it doesn't show on the log? Can you help me?
Hello there! I'm sorry you're having trouble. The thing is.. it's kind of hard to tell why it's not working just from these small lines of code... I'll read this again in a little while to see if there're typos (I just woke up.. not really "lucid" enough for this xD ) but if you want, if your game is still in the prototyping stage and not that "built up" you can send me an email at "chosenplaystudio@gmail.com" with a unity package so I can open it up in unity and see if there's something I can help you with... no guarantees tho :)
when i understand it right his method wont work without the new Input System. the Method Attack() is never called or is it?
InputAction could not be found, what should I do?
Same here...
@@Assistiai make sure to use unity library by writing ( using UnityEngine.InputSystem ) like in his video
Same here..
I solved :) Go window -> package manager -> install inputsystem thx lot
That's because I was using the INput system. Tutorial on that just published :) ruclips.net/video/kgZbkbMkbIY/видео.html
I managed to make it work with old input system but most times it stop working during animation Attack three (Can Recieve Input bool untick and stay that way) I will continue working on it or maybe you guys will not have the same problem but if you guys have it and know how to fix it please help me :D
void Update()
{
if (Input.GetKeyDown("x"))
{
Attack();
}
}
public void Attack()
{
if (canRecieveInput)
{
inputRecieved = true;
canRecieveInput = false;
}
else
{
return;
}
}
I'ìm also working on perfecting in for the tutorial serie that'll come out as soon as I have all assets ready :)
@@chosenplay6896 Great thank you , I made it work with no problems and implemented damage and also works great with touch controls
@@jonathanmora8208 How did you fix the canReceiveInput thing where it stays false after attack 3? I have the same problem
@@christhedevYTsry for late reply , after if(!canRecieveInput)
{
canRecieveInput = true;
}
Here there is an else I deleted it and now my code works without problem
The combo works but after a few punches the canReceiveInput bool is permanently set to false and because of that nothing happens afterwards
Definitly something wrong in your implementation.. sadly not having the project myself I can't tell what exactly :(
Same here can you solved ?
I solved write your button code
canreceiveinput = true;
For me it was a diffefent error
I removed the else return 0 and it worked
It dont lets me write using unity engine. InputSystem
Sorry for the late reply. Made an improved version of this that might help you out.. also you can check out the tutorials I made about the unity input system and those might help you get a better understanding on how to set the inputsystem up :)
are you using new input system or old?
because its not fvcking working
thanks for the video, it's amazingly easy. new subscriber
I only have one problem, when the player presses the attack button many times in a row, it performs 1-2 more attacks (input lag). If someone can solve it, it would help me a lot to know how I do it, thanks in advance.
sorry for the terrible English, I speak Spanish.
An upgraded tutorial on this will soon be released. I've just finished a job and now I'll be starting to work on it :)
@@chosenplay6896 thank you very much, your videos have helped me a lot
In case it works for someone, I managed to solve the input lag by adding a script to the last attack of the combo, and placing an almost imperceptible combo cooldown for the player (0.4)
Thank you. I hope you get rich.
Giving my family a decent life would be enough. But thanks :D
There is an error in combat manager (4,30)
I've just finished a little job I was given. This means that the next days I'll be working on an upgraded version of this tutorial. Soon to be published :)
make this for enemy
It literally chance nothing if not that instead of the Input starting the action you start it with some kind of logic.. like calculating the distance from the player and if distance
When Finish the tutorial test the game it wasn’t working
Definitly something wrong with your implementation.. sadly I can't say more than that tho not having the projhect in front of me :(
Don't do drugs kids.
Im from roblox i only know lua xd
this is such a bad toturial, constantly sped up and I'm getting errors for no reason