Godot 3.0: Top-down Tank Battle - Part 3

  Рет қаралды 31,759

KidsCanCode

KidsCanCode

6 жыл бұрын

Welcome to a new tutorial series. Many of you have requested a full game tutorial, so I've chosen a top-down tank game. I have no idea how many parts this will be, but as we go we'll cover things like tilemaps, pathfinding, ai/behavior, and many other topics.
Art by Kenney: kenney.nl/assets/topdown-tanks...
Follow the written version of the series here:
kidscancode.org/blog/2018/04/g...
Code for this part can be downloaded here:
github.com/kidscancode/topdow...
Support me on Patreon: / kidscancode

Пікірлер: 115
@lincolnpepper816
@lincolnpepper816 6 жыл бұрын
This tutorial is incredibly helpful! It answers two problems I was stuck on for so long. Thank you!
@askaranarbekov3145
@askaranarbekov3145 6 жыл бұрын
did you succeed?
@lincolnpepper816
@lincolnpepper816 6 жыл бұрын
I did solve those problems.
@OngoGablogianTheArtCollector
@OngoGablogianTheArtCollector 3 жыл бұрын
Thank you so much! I really needed the part about rotating the turret for my game! :)
@critacularofficial2374
@critacularofficial2374 5 жыл бұрын
Wow... You, sir, are a saint and scholar; thank you for your benevolence!
@mylegguy8115
@mylegguy8115 5 жыл бұрын
Hold my hand through these, you are making me think too hard and I get lost. I will not give up!
@Bananeisafree
@Bananeisafree 6 жыл бұрын
Very well presented, as always. Cheers!
@IustinNitza
@IustinNitza 3 жыл бұрын
Thx, finally managed to made it work with my very different sprites. It's a pain in the ass to manage all these different rotations :)
@j.d.waterhouse4197
@j.d.waterhouse4197 6 жыл бұрын
Terrific Chris!
@pomegranate8593
@pomegranate8593 10 ай бұрын
lovely stuff!
@adruss99
@adruss99 6 жыл бұрын
Great series!!! The EnemyTank is rotating the turret but not in the direction of the Player. I'm not getting an error and my syntax matches what you have. Thoughts? Thank you in advance!
@fhadi-qf4fi
@fhadi-qf4fi 4 жыл бұрын
A great tutorial, I like it
@ChupoCro
@ChupoCro 4 жыл бұрын
Another way of thinking when writing the aiming code: #$Turret.global_rotation = current_dir.linear_interpolate(target_dir, turret_speed * delta).angle() $Turret.global_rotation = lerp(current_dir, target_dir, turret_speed*delta).angle() #if target_dir.dot(current_dir) > 0.9: if abs(target_dir.angle() - current_dir.angle()) < acos(0.9): shoot() Or deg2rad(25) instead of acos(0.9)
@Cyrclic
@Cyrclic 5 жыл бұрын
Great tutorial, however i have some questions. 1. If i placed my player facing directly with enemy tank (0 rotation, facing right while enemy facing left) within the path, the enemy tank can't push, it will collide, stop and teleport to the back of the player half second later. If i rotated slightly, enemy tank can push me. What is actually happening with the kinematic body interaction? 2. Is method angle() only work with Vector2(1,0), because i made a mistake before using Vector2(0,1) instead and the turret rotation is spinning like crazy with 90 deg interval?
@greggygeorge8198
@greggygeorge8198 4 жыл бұрын
Can you use Path 2D in side-scrolling or when there's a gravity?
@gustavolorenz4883
@gustavolorenz4883 5 жыл бұрын
Hello!!Great tutorials!! Iam making a game a little different from yours, instead of just aim to the player iam trying to do the tank follow him, so for i had no problem, but after the enemy leaves from his pathfollow code and follow the player when the chase ends he just jump back to the pathfollow in a weird position...Do you have any sugestion to fix his return? This is the code for follow the player: if target: var enemy_origin = $".".get_global_transform().origin var player_origion = target.get_global_transform().origin var offset = player_origion - enemy_origin move_and_slide(offset.normalized() * speed)
@adamsmiddy
@adamsmiddy 5 жыл бұрын
Not sure if you check these comment sections still but I have been having issues with the vectors in my game for some reason. In order to get the turret to properly aim at the player, my equations have to be backwards: "turret.global_position - target.global_position" I have attempted to write my own "aim at target" codes before but always have to do this backwards vector subtraction. Not sure where to begin looking into why this is. Also, side note, in the detect on body entered, i would get the error: "Invalid get index 'name' (on base: 'Nil'). I seemed to have fixed it when I deleted the typed script I had added. Not sure if that's OK or not
@mugenmasters8564
@mugenmasters8564 Жыл бұрын
.linear_interpolate is an invalid function?
@diegomata1062
@diegomata1062 2 жыл бұрын
Why the variable parent = get parent?
@parishna4882
@parishna4882 5 жыл бұрын
omfg.. went mental for almost half an hour dissecting the code, why wont the damn turret follow me, was getting strange errors that I could not work out, and who knows.. I copied the var target_dir code and commented out the initial one, and it didn't throw an error.. put the original back, still no error.. But alas, none of that was my issue. Turret wont rotate with a rotation speed of 0... holy buckets.. lol whew..
@duysonnguyenbui496
@duysonnguyenbui496 3 жыл бұрын
Me too, when I first moved into the target area it was quite smooth but then it deflected by a rather large angle from where my tank was moving
@askaranarbekov3145
@askaranarbekov3145 6 жыл бұрын
I'm sticking together collision shape 2d detect rafius and body , and so my tank tower does not rotate properly, how can I fix the gluing?
@Kidscancode
@Kidscancode 6 жыл бұрын
I'm afraid your question doesn't make any sense.
@rahulc4281
@rahulc4281 4 жыл бұрын
Hey! Great tutorial. I have a question. I want my tank to follow the path, but as soon as it detects the player it should deviate from path and start following player. How do i implement this? Thank you.
@Kidscancode
@Kidscancode 4 жыл бұрын
kidscancode.org/godot_recipes/ai/changing_behaviors/
@leeosub1647
@leeosub1647 5 жыл бұрын
at 15:47, on line 24 from EnemyTank.gd, could you explain more on why you wrote Vector2(1,0) to receive the current direction of the turret? in
@Kidscancode
@Kidscancode 5 жыл бұрын
(1, 0) is a unit vector with an angle of zero degrees. Rotated, it becomes a unit vector pointing in the turret's direction.
@Bianchi77
@Bianchi77 4 жыл бұрын
Good one :), my "EnemyTank" is not going straight, is it because of my "PathFollow2D" is not straight ? thanks
@maktrone-learning611
@maktrone-learning611 4 жыл бұрын
yeah
@ArtER21
@ArtER21 3 жыл бұрын
my project is following this tutorial but with my own assets and small differences like turret speed and larger detect radius. the enemy turret only really points at the player when they are parallel on the y axis , if the enemy goes past the player along the x axis the turret is always pointing to one side or the other but not at the player, for the life of me I cant see why that is.
@duysonnguyenbui496
@duysonnguyenbui496 3 жыл бұрын
Me too, when I first moved into the target area it was quite smooth but then it deflected by a rather large angle from where my tank was moving :((((
@hugomartinez2554
@hugomartinez2554 4 жыл бұрын
Hello, I have a problem when the enemy looks at the player, its always looking 90 degres away from te player, I did that part of the script exactly as in the video, but I dont know how to fix it
@Kidscancode
@Kidscancode 4 жыл бұрын
Sounds like your sprite is just rotated. At 0 rotation it should be pointing to the right. Otherwise you'll have to add an extra 90 degrees to the calculation.
@memzen8101
@memzen8101 6 жыл бұрын
at bottom of the written part 3 page the code link goes to part 2 instead of part 3 code
@Kidscancode
@Kidscancode 6 жыл бұрын
Thanks - fixed!
@MsKocagoz
@MsKocagoz 6 жыл бұрын
Sir, do you planning a tutorial for click and move or click select, I want to make some modification.
@Kidscancode
@Kidscancode 6 жыл бұрын
Have a look here: docs.godotengine.org/en/latest/tutorials/2d/2d_movement.html I wrote this doc to answer common 2D movement questions. There's a section on click-and-move
@MsKocagoz
@MsKocagoz 6 жыл бұрын
Mahtan Sirfalas yokum, hayirdir olay varsa gelirim
@mattgoodman2687
@mattgoodman2687 5 жыл бұрын
Could you explain what linear_interpolate means/how it's used? Thanks in advance!
@Kidscancode
@Kidscancode 5 жыл бұрын
"Linear interpolation" (abbreviated as lerp) is nothing more than finding a point between two given ones. The function lets you pick a given amount between two values. For example: linear_interpolate(0, 10, 0.5) would result in 5 linear_interpolate(0, 10, 0.75) would result in 7.5. One common use is to smoothly animate a value from a start point to an end point, such as we are doing with the rotation of the tank's turret. Here's a full in-depth article: en.wikipedia.org/wiki/Linear_interpolation
@mattgoodman2687
@mattgoodman2687 5 жыл бұрын
​@@Kidscancode That cleared it up. Thank you so much!
@ProfElDruidos
@ProfElDruidos 6 жыл бұрын
Thx for this tutorial !! The line var current_dir = Vector2(1,0).rotated( $Turret.global_position ) gives this error : Invalid type in function 'rotated' in base 'Vector2' : Cannot convert argument 1 from Vector2 to float Any clue ?
@Kidscancode
@Kidscancode 6 жыл бұрын
That's because the line should read: var current_dir = Vector2(1, 0).rotated($Turret.global_rotation) We are rotating by the turret's rotation - "rotated" doesn't take a position (Vector2)
@ProfElDruidos
@ProfElDruidos 6 жыл бұрын
Ok I'm blind :`) Thank again for this awesome tuto !
@mitchellstephens4228
@mitchellstephens4228 4 жыл бұрын
can anyone explain why this line fixes the issue of the collision throwing the enemy tank into a weird loop? ``` position = Vector2() ```
@sonhador807
@sonhador807 4 жыл бұрын
With this line you always put local coordinates of tank at the point (0,0), so it means that if our tank push enemy's one, it still will have the same coordinates as the PathFollowing2D
@QuiteVeryJohn
@QuiteVeryJohn 6 жыл бұрын
Hello! First of all, just want to say that I'm loving the tutorial so far. Really helpful stuff but I've run into a bit of a problem. I've created the Area2D (renamed DetectRadius) and CollisionShape2D nodes for detection and I've selected the circle for the shape. I've exported the variables as shown below and entered their values in the Inspector: export (float) var turret_speed export (int) var detect_radius But when I add this line and try to run the Map scene: $DetectRadius/CollisionShape2D.shape.radius = detect_radius It crashes and I get this message in the debugger: Invalid set index 'radius' (on base: 'CircleShape2D') with value of type 'Nil'. I've tried redoing it step by step and it keeps throwing up this error and I'm not quite sure why it would be returning a message like this. Any help you could provide would be greatly appreciated! Thanks in advance!
@QuiteVeryJohn
@QuiteVeryJohn 6 жыл бұрын
Also, I've just noticed that even though I've set the turret speed and detect radius in the inspector of the EnemyTank scene, those values in the instance of EnemyTank in the Map scene are still set to 0. Could this be the issue or should I leave the EnemyTank instance settings alone?
@QuiteVeryJohn
@QuiteVeryJohn 6 жыл бұрын
UPDATE: I adjusted the settings in the inspector the EnemyTank instance and it seems to be working fine now. For whatever reason, those values weren't carrying over when I changed them in the EnemyTank scene. I'll bare that in mind in future! Sorry to bombard you with comments!
@Kidscancode
@Kidscancode 6 жыл бұрын
Instances can each have their own properties, so once you set them, they'll not update if you change the source scene unless you click the "revert" button (the circular arrow that appears next to the property).
@QuiteVeryJohn
@QuiteVeryJohn 6 жыл бұрын
Ahh that makes sense! I shall remember that in future. Thanks very much for the help!
@BlazeAceKun
@BlazeAceKun 6 жыл бұрын
i was having same problem ...thanks for telling
@thezyreick4289
@thezyreick4289 5 жыл бұрын
how can I make a RigidBody2D follow the path? every time I try it breaks the physics engine
@moric4677
@moric4677 5 жыл бұрын
if you try to see your object as rigidbody then why u r following a path. or maybe you can use kinematic mode in Rigidbody property and set it to a rigidbody when I collide with something (you have to code for that) rigidbody can only be moved by adding forces , as far I know.
@thezyreick4289
@thezyreick4289 5 жыл бұрын
@@moric4677 yea I ended up coding an A* pathfinding system for it instead.
@surgicc7892
@surgicc7892 5 жыл бұрын
help guys it says expected indented block after "if" func control(delta): if parent is PathFollow2D: highlights this > parent.set_offset(parent.get_offset() + speed * delta)
@Kidscancode
@Kidscancode 5 жыл бұрын
Well, it seems you didn't indent where it expected you to. In GDScript, like Python, indentation is used to denote code blocks. So after an "if" statement, the resulting code should be indented one level (usually with a tab in Godot's editor). Note that if you copy-and-paste code from a web page, it's likely that the indentation will not be correct. Don't copy-and-paste - type the code yourself.
@surgicc7892
@surgicc7892 5 жыл бұрын
@@Kidscancode i had typed it myself. i pressed tab and that worked thanks
@maktrone-learning611
@maktrone-learning611 4 жыл бұрын
hi, I have a problem. Invalid type in function 'rotated' in base 'Vector2'. Cannot convert argument 1 from Vector2 to float. This is my problem. I am coding on Godot 3.2.1
@Kidscancode
@Kidscancode 4 жыл бұрын
"rotated()" takes a number argument. You seem to be passing it a vector.
@maktrone-learning611
@maktrone-learning611 4 жыл бұрын
@@Kidscancode thank you
@ka1e_chips
@ka1e_chips 5 жыл бұрын
Both my enemy tank turret and player tank turret are always 90 degrees off from where I want them to be. The enemy turret points 90 degrees off from my player and the player turret is always 90 degrees off from the mouse. Does anyone know what to do about that?
@Kidscancode
@Kidscancode 5 жыл бұрын
This is explained in the tutorial. Your initial sprite orientation needs to be pointing to the right ie, (1, 0) or else you'll have to add 90 degrees to all direction angles.
@ka1e_chips
@ka1e_chips 5 жыл бұрын
@@Kidscancode Oh wow it's actually you. I'll thank you for doing your thing on KZbin before thanking for you help. Thank you for spreading what must be years of your experience completely freely. You're doing awesome work (I bet you know it too xD). And thank you for getting me to review older videos. I just fixed the sprite orientation after trying to fiddle with the angle in code by adding 3.14 radians and it works. 👌
@Kidscancode
@Kidscancode 5 жыл бұрын
Glad you got it working. FYI, if you were off 90 degrees, that's PI/2 radians. PI (3.14) radians is equal to 180 degrees.
@ka1e_chips
@ka1e_chips 5 жыл бұрын
@@Kidscancode That explains why rotation didn't work. Thanks 😁
@pedropou888
@pedropou888 6 жыл бұрын
Why is this error appearing ?? Parser Error: Identifier not found: target
@pedropou888
@pedropou888 6 жыл бұрын
0:00:00:0815 - Condition ' _debug_parse_err_line >= 0 ' is true. returned: 0 ---------- Type:Error Description: Time: 0:00:00:0815 C Error: Condition ' _debug_parse_err_line >= 0 ' is true. returned: 0 C Source: modules\gdscript\gdscript_editor.cpp:287 C Function: GDScriptLanguage::debug_get_stack_level_instance
@Kidscancode
@Kidscancode 6 жыл бұрын
That seems to say that you don't have `var target` declared.
@mahtansir8775
@mahtansir8775 6 жыл бұрын
I'm sorry for bothering but i couldn't solve my problem here. Checked your scripts too. but still; Invalid operands 'Nil' and 'float' in operator '*' Error is for "EnemyTank.gd:25" Just when the player touched the circle it errors. I checked turret_speed, it is "1" like you did.
@Kidscancode
@Kidscancode 6 жыл бұрын
This happens when a variable has no value. It must be one of your export vars. The error message tells you which one, likely this one: $Turret.global_rotation = current_dir.linear_interpolate(target_dir, turret_speed * delta).angle() Make sure you check the enemy tank instance that's in the main scene.
@mahtansir8775
@mahtansir8775 6 жыл бұрын
Thx for the answer. It helped me. You are good in teaching you got the point just hit 12 with the last sentence. I was checking again and again the turret speed of enemy tank but not in the map01 scene. My brain is about to blow because it's 10th hour of working that's why i am checking the map scene's enemy tank turret speed.... So i can give it a break now and eat something. Oh i can let it go now... Thx again.
@nedjaimes973
@nedjaimes973 6 жыл бұрын
Please make tutorial of shaders.
@microlabig
@microlabig 6 жыл бұрын
+
@Kidscancode
@Kidscancode 6 жыл бұрын
It's on my list!
@cgcube7342
@cgcube7342 3 жыл бұрын
I am getting this error (the line is highlighted in the source code given below): ===> *Invalid set index 'global_rotation' (on base: 'Sprite') with value of type 'Vector2'.*
@cgcube7342
@cgcube7342 3 жыл бұрын
I downloaded the source code and found that: $TankTurret.rotation = current_dir.linear_interpolate(target_dir, turret_speed * delta).angle() I forgot the *.angle()* in the last
@elidelid
@elidelid 5 жыл бұрын
my game crashed and said the error was on : $Turret.global_rotation = current_dir.linear_interpolate(target_dir, turret_speed * delta).angle()
@Kidscancode
@Kidscancode 5 жыл бұрын
But you're not going to include what the error was?
@elidelid
@elidelid 5 жыл бұрын
KidsCanCode I have downloaded your project and there are no problems. Sorry
@elidelid
@elidelid 5 жыл бұрын
maybe there is something I left behind
@mimique_key
@mimique_key 5 жыл бұрын
You may have, as I did, forgot to put the turret speed to 1 in the inspector for the EnemyTank instanced in the Map01 scene PS: I know it's late but if anyone is still having the issue I hope it may help.
@elidelid
@elidelid 5 жыл бұрын
SitronVert oops, it's old question and it's has solved. (Sorry for my bad english)
@antanaspaskauskas9432
@antanaspaskauskas9432 5 жыл бұрын
For those who use 4 or 8 directional animated sprites for movement, get PathFollow2D.rotation each delta to get direction angle you are supposed to face, which you can then use in a function to get correct directional animation ;)
@paltze
@paltze 5 жыл бұрын
please help enemy tank is not moving
@fhadi-qf4fi
@fhadi-qf4fi 4 жыл бұрын
use "+" instead of "*" parent.set_offset(parent.get_offset() + speed * delta) ;)
@abahanparichha2780
@abahanparichha2780 4 жыл бұрын
//global_position // cant find that variable
@Kidscancode
@Kidscancode 4 жыл бұрын
You'll have to include what you've done. "global_position" is a built-in variable for all Node2D derived nodes.
@fikrilazuardi2049
@fikrilazuardi2049 6 жыл бұрын
i got an error said: Identifier not found: target how to fix that?
@Kidscancode
@Kidscancode 6 жыл бұрын
That error happens when you try and use a variable that you haven't declared. You're missing "var target = null" somewhere near the top of your script.
@fikrilazuardi2049
@fikrilazuardi2049 6 жыл бұрын
KidsCanCode aah okay, so the variable should be written on the top of the script first?
@fikrilazuardi2049
@fikrilazuardi2049 6 жыл бұрын
KidsCanCode can i ask something about GDScript?. what programming language I should learn to understand the gdscript?
@Kidscancode
@Kidscancode 6 жыл бұрын
GDScript is very closely based on Python. You can find lots of Python learning material, and it's a good all-around language to know.
@fikrilazuardi2049
@fikrilazuardi2049 6 жыл бұрын
thank you for the guide. very appreciate it
@ThePillowGamer
@ThePillowGamer 6 жыл бұрын
Mine says target is not found? Why is this?
@Kidscancode
@Kidscancode 6 жыл бұрын
You'll have to be more specific about the problem. Is that exactly what the error says?
@xaiross
@xaiross 6 жыл бұрын
Got the same error: "Parse error: Identifier not found. target" in func _process(delta) (line 19 or 20 in the EnemyTank script)
@Kidscancode
@Kidscancode 6 жыл бұрын
"identifier not found" errors happen when you don't declare a variable, in this case "target".
@xaiross
@xaiross 6 жыл бұрын
KidsCanCode thanks i figureit out
@tcindie
@tcindie 6 жыл бұрын
I've just created a pull request that exposes the path color to the editor UI with a color picker. github.com/godotengine/godot/pull/18163
@Kidscancode
@Kidscancode 6 жыл бұрын
I just saw it. Nice!
@Huderlord
@Huderlord 6 жыл бұрын
how to install this?
@tcindie
@tcindie 6 жыл бұрын
Until it's merged and compiled into an official build, you'll have to checkout either my branch, or the pull request, and build yourself locally.
@adinobro
@adinobro 6 жыл бұрын
A quicker way to have the enemy tank go round the corner is to change the Curve2D Bake interval to roughly 100. It will make it turn smoothly.
@Kidscancode
@Kidscancode 6 жыл бұрын
Good tip! However, I've found when playing with PathFollow2D that the movement isn't quite as smooth, especially when dealing with the "jump" from last point to first point when your path loops. It still requires some fiddling. Plus, I prefer using the control points because then you can see the actual resulting path.
Godot 3.0: Top-down Tank Battle - Part 4
17:45
KidsCanCode
Рет қаралды 35 М.
Godot 3.0: 2D Visibility with Ray-casting
22:22
KidsCanCode
Рет қаралды 70 М.
Хотите поиграть в такую?😄
00:16
МЯТНАЯ ФАНТА
Рет қаралды 3,6 МЛН
路飞被小孩吓到了#海贼王#路飞
00:41
路飞与唐舞桐
Рет қаралды 76 МЛН
孩子多的烦恼?#火影忍者 #家庭 #佐助
00:31
火影忍者一家
Рет қаралды 52 МЛН
Godot Recipe: Multitarget Camera2D
9:16
KidsCanCode
Рет қаралды 10 М.
Godot Recipe: Adding healthbars in 3D
7:27
KidsCanCode
Рет қаралды 18 М.
When Your Game Is Bad But Your Optimisation Is Genius
8:52
Vercidium
Рет қаралды 1,4 МЛН
Godot 3.0: Top-down Tank Battle - Part 6
16:34
KidsCanCode
Рет қаралды 14 М.
Design and Code a Title Screen in Godot 3 (tutorial)
32:42
GDQuest
Рет қаралды 149 М.
Tierlisting the BEST (and worst) GAME ENGINES
33:51
BiteMe Games
Рет қаралды 207 М.
Хотите поиграть в такую?😄
00:16
МЯТНАЯ ФАНТА
Рет қаралды 3,6 МЛН