Melee Combat System in Unity 2D (With Targeting!)

✅ Get the Project files and Utilities at unitycodemonkey.com/video.php...
Let's make a Melee Combat System with the ability to target specific enemies. This is what I used in Hyper Knights.
store.steampowered.com/app/59...
Character Controller in Unity 2D! (Move, Dodge, Dash)
• Character Controller i...
Simple Enemy AI in Unity (State Machine, Find Target, Chase, Attack)
• Simple Enemy AI in Uni...
Make Awesome Effects with Meshes in Unity | How to make a Mesh
• How to make a Mesh in ...
How to make Damage Popup Text (Unity Tutorial)
• POLISH your Game with ...
If you have any questions post them in the comments and I'll do my best to answer them.
🔔 Subscribe for more Unity Tutorials / @codemonkeyunity
See you next time!
📍 Support on Patreon / unitycodemonkey
🤖 Join the Community Discord / discord
📦 Grab the Game Bundle at unitycodemonkey.com/gameBundl...
📝 Get the Code Monkey Utilities at unitycodemonkey.com/utils.php
#unitytutorial #unity3d #unity2d
--------------------------------------------------------------------
Hello and welcome, I am your Code Monkey and here you will learn everything about Game Development in Unity 2D using C#.
I've been developing games for several years with 7 published games on Steam and now I'm sharing my knowledge to help you on your own game development journey.
You can see my games at www.endlessloopstudios.com
--------------------------------------------------------------------
- Website: unitycodemonkey.com/
- Twitter: / unitycodemonkey
- Facebook: / unitycodemonkey

Пікірлер: 95

  • @CodeMonkeyUnity
    @CodeMonkeyUnity4 жыл бұрын

    Check out this Melee Combat System which is exactly what I used in Hyper Knights! 🌐 Have you found the videos Helpful and Valuable? ❤️ Support on Patreon www.patreon.com/unitycodemonkey or get the Game Bundle unitycodemonkey.com/gamebundle.php

  • @loldude8238
    @loldude82382 жыл бұрын

    You're so underrated. I love your tutorials, thank you so much.

  • @techbytefrontier
    @techbytefrontier4 жыл бұрын

    ty for the tips for each video, very helpful

  • @hanzflackshnack1158
    @hanzflackshnack11584 жыл бұрын

    Concise information as always. Someday could you cover how to code a combo system with precise inputs I.E. Marvel Vs. Capcom, Tekken, Skull Girls etc except in a top down format? Thanks for these CM! you're the best.

  • @457Deniz457
    @457Deniz4574 жыл бұрын

    So nice ! Thanks ! :)

  • @-Constantine-
    @-Constantine-2 жыл бұрын

    hey man nice videos tyou have there. I have a question: is there any isometric real time arpg and mouse movement controlled (diablo-clone) with freeflow combat mechanics? (like shadow of war , assassins creed etc) Im looking for nice and many animations, counter attacks and that smooth cinematic/acrobatic feeling . ty in advance.

  • @JTAGames
    @JTAGames4 жыл бұрын

    Love the video!

  • @neozoid7009
    @neozoid70092 жыл бұрын

    hey CM I have one more question why did you return an Enemy when creating an enemy . I found out that it is also possible to create an object without retuning anything in some cases , can you please explain it to me it would be very helpful for me. thanks And also why did you instantiate a transform and not a gameobject ??

  • @darkman237
    @darkman2374 жыл бұрын

    What if you wanted to scroll the combat text on a panel?

  • @pekrupienyu2916
    @pekrupienyu29164 жыл бұрын

    Hi code monkey, great tutorial. I got a question. Why do we use this approach over using 2D colliders on the individual enemies? I understand this current game where there are lots of enemies this approach is objectively better but say for a game with 1v1 fights would it be better or worse to use 2D colliders instead?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    Sure you can place a collider in the attack position and look for what it hits, or even better just do Physics2D.CircleCast();

  • @pekrupienyu2916

    @pekrupienyu2916

    4 жыл бұрын

    @@CodeMonkeyUnity cool. Appreciate the reply

  • @xerodeus2337
    @xerodeus23373 жыл бұрын

    Pretty new to this, just getting started - does this use the new input system from Unity? or the old one?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    It uses the old input system but you can easily convert it, the melee logic is the same

  • @djdatore
    @djdatore3 жыл бұрын

    Hey there, I am rather new to unity and was hoping to find a better way(then with timers) to let the attack animation finish before returning to idle. I don't want to manage timers for every attack(if possible). Appreciate your guidance thanks!

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    You could just make a proper timer class with some simple exposed functions to start a timer and get a callback. Something like: public void StartTimer(float time, Action onTimerElapsed);

  • @truth8483
    @truth84834 жыл бұрын

    Can you make a video on weapon system as part of player attack? Maybe how equipment can be accounted for into the player attack and animation system? For example, melee attack verse ranged attack versed caster magic? One uses a sword, the other a bow, and the last magic which may or may not have a weapon equiped?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    Depends a lot on how you set things up, you need to break that problem into smaller pieces, first handle the animations, then handle the sprites, then handle unit stats and so on. kzread.info/dash/bejne/m6d1z9GGY7W2daw.html

  • @juicedup14
    @juicedup14 Жыл бұрын

    are your animations basically 8 cardinal direction? or are they procedural animations?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    Жыл бұрын

    They are 8 animations, made using my mesh animation system unitycodemonkey.com/video.php?v=11c9rWRotJ8

  • @KennyClipz
    @KennyClipz Жыл бұрын

    Hi, how do i go about making the character face the attack direction after an attack instead of snapping back to its last moveDirection. I've been stuck at it for hours and can't seem to figure it out. Thanks :)

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    Жыл бұрын

    That depends heavily on what animation system you're using, but you would just basically store a Vector3 for the lastAttackDirection and default to that animation while not moving.

  • @sonicherores
    @sonicherores4 жыл бұрын

    A quick question When I go to Instantiate(GameAssets

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    That class was made here kzread.info/dash/bejne/aXuXp7lvpdq1aKg.html

  • @gamerrepublicof4726
    @gamerrepublicof47262 жыл бұрын

    Code MonKey!! you are always the best, will you consider making a top down shooter game like Archero??

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    2 жыл бұрын

    I'm not familiar with that one but I did do a Top Down Shooter a long time ago kzread.info/dash/bejne/d62slaefodTMfdo.html

  • @drspandau8350
    @drspandau83504 жыл бұрын

    Can you please make a tutorial for this Character Base animation thing?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    Yes I covered it here kzread.info/dash/bejne/Y2WXm9SQgtLaeps.html

  • @neozoid7009
    @neozoid70092 жыл бұрын

    CodeMonkey I have one Question why did you take attackPosition = (transform.position + mouseDirection ) ? mathematical explanation .

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    2 жыл бұрын

    In order to attack towards the mouse So if you click above and to the right of the character it will find hte closest enemy in that direction, if you leave the player in the same position and click under and to the left it will find another enemy in that direction

  • @neozoid7009

    @neozoid7009

    2 жыл бұрын

    @@CodeMonkeyUnity Thanks I got it.👍 But How transform.position + mouseDirection leads to mouse direction ? Addition of a position vector and a direction vector leads to the direction vector , but how??

  • @Danny1y5
    @Danny1y5 Жыл бұрын

    On 16:00 when I try to spawn enemies, the object being instantiated is null. Which I think is because of GameAssets.i.pfEnemy. I have the GameAssets script, do I put it on the enemy or spawner object? Both don't work for me when I assign the prefab to pfEnemy.

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    Жыл бұрын

    Make sure you're assigning the reference on the GameAssets prefab, not the object on the scene. Or assign it to the object on the scene but then also save the prefab Depending on the script order execution it might use the object in the scene or it might instantiate a new GameAssets prefab.

  • @Danny1y5

    @Danny1y5

    Жыл бұрын

    @@CodeMonkeyUnity It still seems to come up as null when assigning the reference with the prefab. I've tried putting the prefab in a "Resources" folder too but nothing seems to work.

  • @Fyres11
    @Fyres113 жыл бұрын

    Do you have a rear attack? I mean extra damage if you hit the enemy from behind?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    If you have a vector to where the enemy is looking at and you have the player position you can calculate the Dot product and if its < 0 then the player is behind

  • @Fyres11

    @Fyres11

    3 жыл бұрын

    @@CodeMonkeyUnity I did your turn based battle system. Does it have that in the scripts?

  • @deeptiyadav923
    @deeptiyadav9233 жыл бұрын

    Hey can you make armor tutorial

  • @ozancobanoglu812
    @ozancobanoglu8124 жыл бұрын

    I'm Thinking This Camera Is Cool But But Most Of The People Wants To Create Complete Platformer / metroidvania but unfortunately most of the tutorials are incomplete I wish you can make some on udemy or on KZread

  • @mehmedcavas3069
    @mehmedcavas30694 жыл бұрын

    Hey code monkey what about a 2d Minecraft game like terraria with simple inventory and crafting system. A small series would be great

  • @lethn2929

    @lethn2929

    4 жыл бұрын

    As it turns out he's already done a crafting system tutorial. kzread.info/dash/bejne/fqGFmLdsic6ueM4.html

  • @mehmedcavas3069

    @mehmedcavas3069

    4 жыл бұрын

    Violet twd hey dude 😊👋🏻

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    Yeah a Terraria like game is something I was actively working towards but I haven't covered many sidescrolling systems yet so I had to postpone that project. I was preparing for a normal Top Down Survival Crafting game but then got caught up with the DOTS update so haven't been back to it. There's lots of cool stuff I want to make, just not enough time to do them all!

  • @OutBoy12123
    @OutBoy121233 жыл бұрын

    Hey Code konkey I really need your help I want to make a combat system that makes you go in combat mode when you click a button so like there are two modes one for combat other for pasif actions Please Help ı really need this

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    Sounds like you need to define a State machine. I covered something similar in the Enemy AI video kzread.info/dash/bejne/lpZkrbmSkbrMlbA.html

  • @OutBoy12123

    @OutBoy12123

    3 жыл бұрын

    @@CodeMonkeyUnity Yes! THANK YOU but can I define the same thing for the player and not with AI instead with a input button I am fairly new to unity and you are the best at teaching

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    @@OutBoy12123 Sure, instead of switching states automatically you do it when the player presses a button

  • @OutBoy12123

    @OutBoy12123

    3 жыл бұрын

    @@CodeMonkeyUnity Thank you so much You are the best out there about this topic also my game dev idle

  • @FIFAGLITCH
    @FIFAGLITCH3 жыл бұрын

    Hi do these codes work on mobile or only windows and linux?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    The only difference between Desktop and Mobile is the Input method changing from Keyboard to Touch. Everything else is the same

  • @FIFAGLITCH

    @FIFAGLITCH

    3 жыл бұрын

    Code Monkey Hi So i should copy your toturial and it will work? And what is the input method?

  • @husejinbg
    @husejinbg4 жыл бұрын

    What is your camera size ?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    About 70, the number itself doesn't matter, it's all relative to the size of your objects.

  • @gower1973
    @gower19734 жыл бұрын

    Isnt it bad practice to just allow the player to spam the mouse to attack, wouldnt it be better to have a timer to limit the number of attacks, like attacks per second or some such mechanism.

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    Depends on what type of game you're making. If it's meant to be like Dark Souls then you should let the animation play fully, if it's meant to be fast paced you can let the player attack at every click.

  • @sovietonionisthebestonion3805
    @sovietonionisthebestonion38054 жыл бұрын

    Can you make your player jump ?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    Sure kzread.info/dash/bejne/oqiqrZZ_oJjcgrw.html

  • @nathan12581
    @nathan125814 жыл бұрын

    Tutorial on your ‘animation system’?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    It's based on dynamic meshes, I covered it here kzread.info/dash/bejne/Y2WXm9SQgtLaeps.html

  • @DRATER469
    @DRATER469 Жыл бұрын

    Does anyone know the video where he shows how the attack is animated?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    Жыл бұрын

    MY animation system is based on dynamic meshes unitycodemonkey.com/video.php?v=11c9rWRotJ8

  • @DRATER469

    @DRATER469

    Жыл бұрын

    @@CodeMonkeyUnity ahh i was hoping that wasnt the case. Cant wrap my head around that one for some reason. Ill give it another go!

  • @christopherdoiron4294
    @christopherdoiron42943 жыл бұрын

    This is the only video on the internet I can find that explains how to targeted melee attacks, yet, if you follow this tutorial, you'd have to write an entire library of other utilities and animation scripts. Each one with leads back to previous videos. At this point, trying to get my character to have targeted melee becomes useless because I might as well just pretend my game is your game. Solution: is there a simpler way of rotation an animation from the animator?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    The logic for finding the target is what matters, calculating the direction and identifying nearby objects, that logic can work with any animation system you want.

  • @AkashKumar-gu7jc

    @AkashKumar-gu7jc

    2 жыл бұрын

    Well, you could take this video as an inspiration and try to implement it in your game, the key is the ability to learn from ideas and make your own solution.

  • @KalponicGames
    @KalponicGames3 жыл бұрын

    how do you make a player jump in 2D top-down game?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    Depends on how you want to implement it since there's no "natural" way to display that

  • @KalponicGames

    @KalponicGames

    3 жыл бұрын

    @@CodeMonkeyUnity can you make a video of how to do it? Also I am unable to reach you for some questions I had. Do you provide any tutoring?

  • @sgillespie00
    @sgillespie003 жыл бұрын

    Your videos are so awesome, but I have to know: are you really typing in real time?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    Usually I speed up the sections where I'm just typing and not talking in order to keep the video flowing. But when I write and talk at the same time yeah that's my normal typing speed. Been writing code for 20 years!

  • @nontasng8068
    @nontasng80682 жыл бұрын

    The codemonkey utils have a lot of errors I dont know what to do

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    2 жыл бұрын

    What errors?

  • @nontasng8068

    @nontasng8068

    2 жыл бұрын

    @@CodeMonkeyUnity Camera errors mostly in util folders

  • @nontasng8068

    @nontasng8068

    2 жыл бұрын

    @@CodeMonkeyUnity Also I would love a video explaining the codmonkey utils I prefer learning without using utils and packages

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    2 жыл бұрын

    ​@@nontasng8068 Are you talking about the Utils or project files? The project files uses Cinemachine, but the Utils themselves don't need anything.

  • @TheJoppemin
    @TheJoppemin3 жыл бұрын

    5:05 is a bit confusing, isn't explaining the contents of that function super relevant for the video?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    The animation system is completely separate from the Combat System, in my case I'm using my custom animation system but you can use any animation system you want and combine it with this combat system.

  • @TheJoppemin

    @TheJoppemin

    3 жыл бұрын

    @@CodeMonkeyUnity ah ok, it was more the angling of the animation that I was interested in but I assume it just rotates the root of the arms with transform.up or something similar?

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    3 жыл бұрын

    @@TheJoppemin In my case I simply have 8 animations: Up-Left, Up, Up-Right, Left, Right, Down-Left, Down, Down-Right So they don't actually perfectly connect like if I were using IK but it's close enough.

  • @lethn2929
    @lethn29294 жыл бұрын

    I love how I did all this research on how to do 2D targeting with a mouse, implement it and then fucking Code Monkey comes along with a tutorial explaining it all in exactly the same way :( he's as bad as Brackey's, he's even doing the same maths to make the raycast accurate.

  • @457Deniz457

    @457Deniz457

    4 жыл бұрын

    Lel.. so ur mad bcs u "wasted" time or bcs of that others can do the same thing in a fraction of the time u had invested?^^

  • @CodeMonkeyUnity

    @CodeMonkeyUnity

    4 жыл бұрын

    It's very important to develop the skill of doing your own research so all the time you spent wasn't wasted!

  • @457Deniz457

    @457Deniz457

    4 жыл бұрын

    @@CodeMonkeyUnity Yes ! :) Thats why I said "wasted" :P

  • @lethn2929

    @lethn2929

    4 жыл бұрын

    @@457Deniz457 Yes exactly lmao, I don't hold it against them, this is how technology and coding works but it is such a piss take when a video for my problem gets uploaded moments later. I think it was actually a couple of days ago I dealt with the issue and had to find some obscure unity forum post on google of course.

  • @457Deniz457

    @457Deniz457

    4 жыл бұрын

    @@lethn2929 Well I feel you. I also have to struggle thing where I know in 1-2 years everone can quick google it ... xD

  • @subliminalcastillo2126
    @subliminalcastillo21263 жыл бұрын

    You know a game dev doesn't fuck around when he codes in every single mechanic in his games.