Adding a Projectile (Combat Essentials in Godot | P4)

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments

Nice tutorial

👍︎︎ 2 👤︎︎ u/DevOcto 📅︎︎ Apr 29 2021 🗫︎ replies
Captions
hey it's ice and in this episode we are going to make projectives and specifically we are going to add projectiles that our player can throw and damage enemies okay so first of all let's create it we're gonna make it on the base of hitbox so go to overlap and hit box here scene new inherited scene choose hitbox rename it to player dagger and now add a sprite to it and let's set it first of all our player dagger it will exist in projectile and it will collide with environment and enemies now let's add the sprites we need it to point straight right so we are gonna rotate the sprite not the dagger but exactly the sprite and now let's set the collision shape and let's save it at a separate folder called projectiles now before everything else let's add it to projectile group because it will be very useful when we will be checking what an object it is extend the script and here create an integer export variable code speed and in the physics process method change the global position by speed multiplied by direction multiplied by delta direction is the result of rotating a vector pointing straight right by the rotation of our dagger for instance if our rotation is zero so it's not rotated so it will go right let's check it as you can see it goes right and if we rotate it a little bit it will go in this direction great okay now let's make it collide and break first of all let's create the method called destroy and it will just destroy our dagger now since it's an error 2d we can connect its signals connect the area entered and body entered signals and in them go the destroy method we have just created you may ask why didn't i call q3 in both of them if it's just one line of code because you might add some functionality later down the road like sound effects or visual effects for destroying a projectile that's why however it will not be destroyed when it leaves the screen borders if it doesn't collide with anything you might encounter a problem when you have a ton of daggers out of your screen you cannot see them but they still exist and where there is a lot of them it becomes really a problem so in order to delete them we will use visibility notifier 2d node just connect its exited signal and in this new method just delete the dagger so you can see it here right now it exists but now it was out and then it was deleted okay so now we can make the player throw them go to player scene open its script and before we start adding the methods go to project settings and the input map add an action called action attack and i'll connect mouse left button for it so this action is called every time we press mouse left button okay and in player script let's make a method called throw dagger i have already written them functional here but i forgot to write the reference so create a reference variable here is our packed scene and here we check if it exists if it's not now then we instantiate the dagger we add it to the note 3 we set the position of it and then we determine the dagger rotation that is based on the direction from the player to mouse position and then we convert this direction into radians and set the rotation of the dagger to this value and in physics process now add this so now our player will throw daggers at any time the action attack is pressed however we can make it a little bit better so we can receive dagger throwing direction and then convert to rotation so here in the brackets write dagger direction of type vector 2. let's copy that and instead of this part right dagger direction and now where we call this method in physics process add this dagger direction direction from the player to mouse and pass it to the throw dagger method let's check it as you can see our player can throw the dagger however you might want to add some cooldown because it's way too fast right and it's quite easy actually go to player add a timer node for it rename it on attack timer set it to one shot and probably let's set it to 0.2 the way it's time in the script add a reference to it here in throw dagger at the very end start this timer and in physics process after you check whether the action was pressed add the next condition and attack timer is stopped so it starts when a dagger is thrown and it stops after 0.2 seconds and then we can throw one dagger again as you can see i can no longer throw it that fast that we could the problem is our daggers are not colliding with the enemy let's check if it deal damage oh it does deal with damage so what's the problem the problem is that our heart boxes are not set to monitorable so they detect the dagger being in their area but the dagger does not detect these harmonics so what's the work around here we can directly call the destroy method from our enemy base which is in fact entity base we need our hardbox area entered method here and here remember i told you let's add our dagger to projectile group we will check if the hitbox is a projectile by using his group and here you go you check if it's in group projectile and destroy it it's working so that was it for this episode i hope it was useful and you liked it and if you did don't forget to like share and subscribe it was ice and until next time [Music] you
Info
Channel: John Ivess
Views: 1,276
Rating: undefined out of 5
Keywords: godot, gamedev, programming, tutorial, gdscript
Id: RBvK5lQJHAs
Channel Id: undefined
Length: 7min 9sec (429 seconds)
Published: Wed Apr 28 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.