HOW TO MAKE A 2D RANGED COMBAT SYSTEM - UNITY TUTORIAL

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
welcome back to black thorn prod I'm Noah now a few weeks ago I made a tutorial on ways you can go about making a melon combat system with unity and c-sharp and so I thought I only normal to follow up with a ranged combat video by the end of this tutorial you'll have a character capable of handling a ranged weapon that rotates to face the mouse cursor and then shoots cool looking arrows that kill enemies of course you'll be able to a plan you learn during this video to make all kinds of range shooting systems from huge powerful bullets and rockets to delicate little arrows now everything taught here can be applied to any game in need for a shooting system without a child I'm sure or a side-scrolling platformer so as you can see I've set up this little seam with an archer like character who carries a simple ranged weapon and I can move left and right I also got an enemy character I'll use has targets first of all I want to get this weapon rotating so it faces the mouse cursor so I'll create a new C jump script called weapon drag and drop it onto the weapon and open it up now notice that the small chunk of code were about to write to get the weapon rotating looks a little alien but they're not necessarily very easy to understand but don't worry once it's done the rest will be a lot easier so you basically need to calculate the angle we need to rotate around so that our weapon points towards the mouse cursor we do so by first calculating the direction between the weapon and the mouse cursor remember that to get Direction all you need to do is subtract the destination position at all the cursor with the origin which is a humectant position now here comes the tricky line of code in which we will calculate the amount of degrees our weapon must rotate to face their cursor using that direction vector because now we know in which direction the weapon should be facing so we'll set how many degrees must rotate around to reach in that direction lastly we can set the weapon rotation with zero in the x and y for the z axis which is the axis we want our weapons rotate along I'll type rotten Z which is the amount of degrees we established here plus offsets which is a public floor variable I'll treat right here this is in case the weapon graphic doesn't seem to be facing the mouse cursor as we can tweak its rotation with offsets so that it does so back inside of unity outside - 94 offset which is a value that works really great for me and you'll see that indeed my range weapon faces the mouse cursor great so as you saw that was a little tricky the race will be a lot easier I can assure you is now time to give this weapon the power to shoot projectiles so I'll create a public game object variable called product I'll whenever the player hits a left mouse button I'll spawn a product I'll I'll spawn an added tip of my weapon so I'll make a public transform variable called shot pose and use there for my projectile starting position has a rotation I'll spawn the projectile with the same rotation has the weapon back inside of unity I'll turn this simple arrow sprite into a prefab and then drag and drop that newly made prefab inside of this product I'll slot in the inspector I also make an empty game object called shot pause appearance into the weapon today moves and rotates with it and then drag and drop it inside of that empty shop or slot and you'll see that when I click the left mouse button tronic tails will it need spawn with a correct position and rotation if your rotation does look however a little bit off make sure that your product has default rotation in your sprite sheets is facing straight upwards okay let's now make a simple timer system to control how fast the player can shoot product Isles to do so make a private throw variable called time between shots and the public flow variable called start time between shots before allowing the player to shoot a product I'll check whether the time between show is less or equal to 0 if so you can shoot a product I'll and then I'll reset time routine shots equal to start time between shots which is a value we will set in the inspector if not slowly decrease that value using - equals time that's Delta time back in unity I'll allow the player to shoot a projectile every 0.25 seconds excellent it's now time to get these product Isles blasting forwards I'll create a new C job script called project I'll place that on my product I'll prefab and open up I'll begin by creating a public float variable called speed if it will dictate how fast the product I'll will move forwards and then in my update function I'll get the product I'm choosing forwards using the transform the translate function the direction being transformed or multiplying with speed multiplied by time delta T if you want to give your product I'll a limited lifetime you can also easily do so just make a public float variable called lifetime and then invoke a function called destroy product aisle after lifetime amounts of seconds in this destroy trial function understand sheet a little particle effects to make the projectile destruction look cool and then of course I'll destroy my product I'll back in the unity editor I'll give my product I'll a speed of 10 for example a lifetime of 1.5 then I'll drag and drop my effect in this empty slots and hitting play you'll see that indeed my projectile shoots forwards and will destroy itself after 1.5 seconds awesome now if you're making a fast-paced action game I recommend you make your bullets fireball or arrow big and powerful again also make it fast so it doesn't appear floaty and weak some people will probably roll their eyes and shake their heads now you can also add some screen shade whenever you should project I'll to make the attack feel more impactful of course spawning a little particle effect near the tip of the weapon whenever you shoot will also make things feel that much more cool and satisfying with that said let's get the product I'll actually hurt Singh enemies there are many ways you can go about doing this in this tutorial I'll be using rays which are basically invisible lines that can detect colliders to create array I'll type in my update function recast hit 2d I'll call my ray hits info and then said that equals a physics 2d recasts and in the parentheses I'll state a start position for my ray I'll just make that my project Isles current position they'll type out in which direction I want my ray shooting out from it'll be transformed up so that the ray blast and frothy arrow I can also set a distance for my radio it's best to make a public floor distance variable I'll set and the inspector for that lastly you can use a layer mask to get your rate ignoring certain liners which is very useful so I'll make a public layer mask variable up here called white solids and plug it in right there now I can check whether or not my rate collides with anything if so I'll also check whether what the Ray has collided with has the enemy tank if it does then I obviously want to deal that enemy some damage for now I'll just be bugged all on Fairfax once the Ray has collided with something whether it's an enemy or not I'll make sure to call the destroy projectile function so that indeed destroys itself awesome I'm gonna head back into unity and type 0.5 for distance in the inspector no need for anything bigger we just want the product I'll detecting nearby colliders and I'll make two new layers one called enemy the other environments and give my enemy the enemy layer my various environment pieces the environment layer I can now state that what is solid is anything with the enemy or environment layers also make sure to create an enemy tag and add that to the enemy character so that my projectile will know it's hit an enemy and do some damage before hitting play to test things out I'll make sure to add a 2d Collider to my enemy character as well as my environments and now it's play and you'll see that when my product I'll hits the enemy or part of the world it will destroy itself and instantiate a cool destructible particle effects let's now deal this enemy some damage I'll create a new C job script called enemy drag and drop that onto the enemy character and open it up in Visual Studio I'll give this enemy a public int variable called health they'll make a public function called save damage that takes in an int damage parameter and I'll subtract health with that damage value and now I can go back to my chronic health script creates an in the variable called damage and if it collides with an enemy then I can call the take damage function and pass in the amount of damage I wish to deal and there we go I can now make an if statement inside of the enemies update function checking whether health is less or equal to zero if so I'll instantiate an enemy death particle effects to make things look cool and destroy the enemy and I'll head back into unity give my enemies three health for example let's drag and drop his death Thanks in this empty slots and then I'll select my product I'll prefab and said damage equal to one and you'll see that after three shots aimed at the enemy character who died in a shower of particles and that will mark the end of this ranged combat tutorial now as you can see you can make a dozen unique weapons with this simple system you can make weapons that take a lot of time to shoot when they do they release a mega powerful fireballs you can also create weapons that shoot very fast but cast tiny little product aisles that deal hardly any damage there's really play around with these start time between shots product tells feed lifetime and damage values and you can come up with all kinds of unique and fun weapons heck you can even see this a step further and give some special product tiles ones destroyed an explosion effects whereby all enemies found in a certain radius from the destroyed product I'll take a certain amount of damage you can also play around with physics and make cool forcing arrows and or I have fun adding recoil to your shootin player-character with that said if you feel like a range combat system part to where I can show you how to do all these things definitely let me know in the comment section down below ok thanks so much for watching if you liked my content then consider supporting me and my channel by hitting the like and subscribe buttons you also support me financially by patreon like these top supporters alright have a great weekend for those taking part in the allotted re Game Jam best of luck and have fun I'll see you sometime soon next week at Fred let up diary behind the scenes video in which I'll share with you all my game my game job experience call okay Cheers [Music] [Applause] [Music]
Info
Channel: Blackthornprod
Views: 222,786
Rating: undefined out of 5
Keywords: unity, tutorial, noaCalice, c#, programming, art, animation, game dev, ranged, combat system, melee, ranged combat system, shooting, projectile, weapon follows mouse cursor, look at mouse cursor, particle effects, 2D, indie game dev, HOW TO MAKE A 2D RANGED COMBAT SYSTEM - UNITY TUTORIAL, bullet, raycasting, collisions, game art, game juice, screen shake, attack, gun, weapon, attack animation, arrow, recoil, enemies, coding, scripting, hitboxes, attack shape, attack speed
Id: bY4Hr2x05p8
Channel Id: undefined
Length: 10min 36sec (636 seconds)
Published: Fri Aug 10 2018
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.