This is one of the most useful Godot tutorials I've ever seen. Great to se somebody making really original and useful videos. Thank you a lot for this!
great tutorial. something to mention is that if the player rotates, their collision shape will rotate as well and you'll need to rotate the corner points around the player position before doing the vector math to get their position in game space
I just started getting back into Godot after over a year and been using Godot 3.1 (and now Godot 3.2) and I just went through this video and the best part is after I first investigated the starter code, I actually learned how to do projectiles from turrets (which is something I didn't know how to do) and now understanding how to do this Raycasting stuff is extremely powerful. I've seen it used for tons of other reasons but never fully got how it worked. Thanks again! I love your teaching style which makes it fun to learn about this :D If you were able to do more series on, say a combat system which could utilize this and stuff in some of your other videos, that would be so awesome :) I know combat is a pretty heavily used mechanic in a number of games and seeing how you would set it up and do it would be beneficial for everyone. Cheers!
You mentioned at the end of this video that a person could do reflections by starting a raycast where the other left off. And then that you could put that into a loop to have it bounce multiple times. I'd love to see one where you showed the code of how to do that efficiently.
I'm getting this error 50% of the time - Invalid get index 'shape' (on base: 'null instance'). How could I make sure that get_node('CollisionShape2D') exists before declaring target_extents var ? If I add an if target.get_node('CollisionShape2D') != null I get some weird behaviour. Thx
Hi, thanks for the tutorial super helpful. One problem with my project is the turret doesn't shoot the player when its radius is touching the platform/walls even when the player is in the radius circle. The walls does have a collision2D shape but looking at your project, i am not sure how you made yours work when the circle is touching the walls.
Hello! I wonder if you'd know some tips to give the player a limited vision range, like the player not being able to see through wall tiles and instead just see darkness. I've already tried to achieve this by using Light2D and LightOccluder2D but this doesn't work as I want it to, I guess I don't want a "flashlight" effect, but more like a true ranged vision. The game "twc byond" achieves this, and I'd like to know how to recreate something like that!
Can you edit it for 3D Environment with a "Cone" Visibility for Enemy (Shape Orientation : point towards enemy and Circle outwards) and "Cylindrical" Visibility for Turret (Shape Orientation : Flat Top & Bottom)? Also can you do an A.I. Pathfinding script.
Sir, can we use raycast to show area of attack? I mean in rpg game when a boss uses skills, normally thare will be circular or rectangular red areas when the attacks will be launched
I have a question. You used your current bit mask layers that you had to know when to stop the ray cast, but I have added another layer and mask layer to my enemy, but the problem is that now if I have multiple enemies chasing me, if one enemy that is tracking my player goes behind another, the line of sight will stop and the enemy in front. How could I specify which mask bits I wanted to detect instead of using all the current mask bits I have selected for my enemy? (In this case I just want the first bit mask and if possible, the second as well)
Whenever I place my enemy and player character under a Ysort node the detection for the enemy npc doesn't work at all (no lines being drawn). Although if I move it oustide of the Ysort node everything works fine.
Could you do a tutorial on how to cast shapes? For example, Unity 3D has a kind of raycast called, "SphereCast." It casts a sphere wherever it's pointing. This is perfect if you want to make your own character controller.
Is there a way, to make a Circular Raycast, or make a Raycast do an instant 360? Because im trying to make a Granade and it would be really helpful for the explosion
If it is possible, maybe do a video about a pop up option menu on mobile devices that appears over any level which uses a minimum amount of cpu when it is not needed.
or you could just create a custom collision bounding box shape one for detect and another for shoot. no rays. no masks. a lot less code (but wouldn't be able to use in different environments without hand making a new detect collision box shape for each placement if positioning to obstacles is different ) just thought be good to mention alternative pro and con.
You seem to have taken a script that starts with "extends CollisionShape2D" and attached it to an Area2D. A script attached to an Area2D must start with "extends Area2D".
Hi Chris! You are awesome!! Could you please make tutorial on creating a top down tile based game in godot 3 (like that you have made for pygame or something new)?
Sure. In my example, I'm using the direction of the raycast to set the direction of the bullets. You could just as well use that direction vector to set your mob's velocity.
Hey men great tutorial, i search on internet and didn't found how to properly set up the collision mask and i end up with my result.collider.name always returning "EnnemyArea" wich is an area for making my ennemy collinding with a bunch of stuff, my raycast seems to hit this area first. I think if i set up correctly my layerMask it should work but i don't find any tutorial for godot 3 may you help me ?
Thanks man, i got it work with exclude everything in the intersect_ray() method. If i understand correctly i have now two layers name player and ennemy, on my player kinematicBody i set the layer to Player and the mask to Ennemy and the opposite for the Ennemy kinematic body and it will work great ?
i could not get the result.collider.name to be compared with the player object. is there any solution to it.. i used player_node = get_parent().get_node("Player") could not compare if result.collider.name == player_node or "Player" or player_node.get_node("PlayerBody") #its collision name could u help me out ASAP.. Thanks in advance..
What does "could not get it to be compared" mean? Did you get an error message? Was your code identical to the code in the tutorial? I can't suggest a solution without knowing what the problem is.
@@Kidscancode I changed the tilemap cell size to 32 along with editing the icon.png down to 32x32 and fixing all of the scalings of the turret and player but now the ray is stopped by some invisible collider I can't find. Here is what I am seeing: imgur.com/VwBnBEr
@@Kidscancode I had to reimport the icon.png to get it to see that it was 32x32 and fixed the shape offset. I am very new to this engine and am trying to learn stuff as I need it.
The thing is i only wanted to do shooting (by main player) and i only use some of your code, and i didn't see var/const pos, so i think it is declared in some other way, dont know where
@@Kidscancode I'm trying to use your concept in my game. Your project works just fine. But in my case i can't get full rotation, it's only works from the left side of the screen. Then i took your code, with all signals, masks... and got the same result. Trying to figure out what is going wrong. Narrow rectangle is like 10 by 70
Certainly. Raycasts have lots of uses. That's one common use of a raycast, although a shape might work better to detect touching a wall. There is also "is_on_wall()".
Hey man. Really thanks for all your tuts. They're insanely good. But for this one it's a bit different. It's good and all, but I'd like to know how it works. So I wanted to reverse-engineered your script. I made a blank project, setup a new character who have different properties than yours. I exported the turrets from your projet and tried to tweak it to make them able to recognize my character... But couldn't make it happen. The turrets don't target this new character of mine and I can't figure out why. For testing purpose I tried to import the character from the ray-casting project to my own... The turrets are able to aim him but do not shoot anything. Is that because importing things from a project to another is broken, or is there specific things in your Main scene or Player that make it works ? I'm going to make turrets step by step after that. But I wanted to try to tweak it a little before trying to code it by myself because I'm a complete beginner with Godot... Once again, thanks for your great tutorials.
Most likely you didn't set up your collision layers like I did, and that's why the turrets don't see the player. This really wasn't intended as a beginner-level demo, which is why I didn't go too deeply into the initial setup. It was specifically to talk about the raycasting technique as applied to a common problem.
First off thank you very much for taking the time to make these videos. Is this compatible with 3.1? I got to the section about adding the directions and received and error on this line: var ne = target.position + Vector2(target_extents.x - target_extents.y) The error reads as follows: error(34,29): No constructor of 'Vector2' matches the signature 'Vector2(var)'. If anyone reads this and/or replies I appreciate it.
Thank you very much for your response. I've been staring at this code for far too long. Hey so on line 27 he has the following line: var target_extents = target.get_node('CollisionShape2D').shape.extents - Vector2(5, 5) Would this still work with a CollisionPolygon2D? @@nailbomb420
@@OsamaBombLyrics No it wont work with a polygon, it's specific to the rectangleshape2d, i.e they're different properties for the different shapes, circleshape2d has radius instead for example. If you used polygon for no real reason, just swap it to a rectangleshape2d, if the polygone is fairly important to the shape of your character, then consider adding an area2d with a rectangleshape and using that for basic stuff like this. When it comes to most collision you will find it much much easier to use rectangles or circles, as the maths is quite simple to figure out intersections. Ass soon as you start to rotate things or use odd shapes it means either alot more more work, or higher level of maths :/ (which is annoying) So I suggest using rectangles where possible. Note, that you can make the rectangle much smaller than the size of the sprite, i.e many 32x32 sprites can have rectangleshape colliders 16x16 or some other dimensions. Hope this helps.
@@OsamaBombLyrics here's a link to the polygon2d class documentation: docs.godotengine.org/en/3.1/classes/class_polygon2d.html the polygon does store all the points that define it, so you could in theory get that list of points and use them instead of the rectangleshapes extents, as they're both just points in space. Might be worth checking out.
@@nailbomb420 Yessss! I got it working! Thanks for the tip and responses! I just used another CollsionShape2D on the Player with its roll to specifically handle interacting with the Turret. I will probably take your advice and switch my CollsionShape2D's to either rectangles or circles. I was using the Polygon2D shapes because I wanted all the collisions to be as exact as I could make them. However, if it will be just a bigger headache in the long run I will just keep things simple. I'd rather have a working game then nothing at all.
There's a link on my website to send me email. Or you can post here in the YT comments if it's a relatively short one. I can't promise I can help you, but I'll take a look.
Sooner or later. There's lots of Python material out there already, and not as much for Godot, and I've gotten very involved with the project so it's my focus right now.
For all these years still this code help people
You are like a hero, the hero of Godot and all you teach is a gift for us. Thank you a lot!!
This is one of the most useful Godot tutorials I've ever seen. Great to se somebody making really original and useful videos. Thank you a lot for this!
Thanks!
great tutorial. something to mention is that if the player rotates, their collision shape will rotate as well and you'll need to rotate the corner points around the player position before doing the vector math to get their position in game space
I love that you did the break at 20:10. I appreciate your video's focus on code quality/efficiency while making an enjoyable game.
I just started getting back into Godot after over a year and been using Godot 3.1 (and now Godot 3.2) and I just went through this video and the best part is after I first investigated the starter code, I actually learned how to do projectiles from turrets (which is something I didn't know how to do) and now understanding how to do this Raycasting stuff is extremely powerful. I've seen it used for tons of other reasons but never fully got how it worked. Thanks again! I love your teaching style which makes it fun to learn about this :D If you were able to do more series on, say a combat system which could utilize this and stuff in some of your other videos, that would be so awesome :) I know combat is a pretty heavily used mechanic in a number of games and seeing how you would set it up and do it would be beneficial for everyone. Cheers!
The tutorial is still very helpful after all the years, thank you.
What a master! And you devise such effective and scalable solutions. Thank you so much for sharing your knowledge with us.
Every video is soooo useful. No matter what you cover, I always learn something I didn't know. Thank you a lot!
Man, your tutorials are great! Be sure of one thing: you are changing lives, man!!! Keep up the great work. Cheers from Brazil. :)
Nice, wish I had tutorials like this when I was 15 and trying to code games on my C64 with Gary Kitchen's Gamemaker
You mentioned at the end of this video that a person could do reflections by starting a raycast where the other left off. And then that you could put that into a loop to have it bounce multiple times. I'd love to see one where you showed the code of how to do that efficiently.
Thank you so much! Probably the best godot tutorials I have ever seen!
One more great tutorial, with your help I will open my own game studio :)
Good luck!
Added another tool in my arsenal as a game dev. Thanks for this!
Awesome! One can never have enough Godot tutorials. ^_^
I needed this video a lot!
I'm making a player-controlled turret that fires LASERS. It's fun.
great tutorial with clear explation of every code
Excellent tutorial and lesson !!!
Hey! I was looking for something like this!! Thank you very much!
Glad it could help!
Very nice video, I prefer these short tutorials over the long series! How did you add custom names to the layers and masks by the way?
In Project Settings -> Layer Names -> 2d Physics
I'm getting this error 50% of the time - Invalid get index 'shape' (on base: 'null instance'). How could I make sure that get_node('CollisionShape2D') exists before declaring target_extents var ? If I add an if target.get_node('CollisionShape2D') != null I get some weird behaviour. Thx
Can you change the circles to a Sector instead?????? Pliz tel me
Hi, thanks for the tutorial super helpful. One problem with my project is the turret doesn't shoot the player when its radius is touching the platform/walls even when the player is in the radius circle. The walls does have a collision2D shape but looking at your project, i am not sure how you made yours work when the circle is touching the walls.
great tutorials! also, writing in comments would be good for reinforcement
Hello! I wonder if you'd know some tips to give the player a limited vision range, like the player not being able to see through wall tiles and instead just see darkness. I've already tried to achieve this by using Light2D and LightOccluder2D but this doesn't work as I want it to, I guess I don't want a "flashlight" effect, but more like a true ranged vision. The game "twc byond" achieves this, and I'd like to know how to recreate something like that!
Really nice stuff, thanks for this!
This is immensely useful. Thank you so very much.
Can you edit it for 3D Environment with a "Cone" Visibility for Enemy (Shape Orientation : point towards enemy and Circle outwards) and "Cylindrical" Visibility for Turret (Shape Orientation : Flat Top & Bottom)?
Also can you do an A.I. Pathfinding script.
Great video as always
Sir, can we use raycast to show area of attack?
I mean in rpg game when a boss uses skills, normally thare will be circular or rectangular red areas when the attacks will be launched
THANK YOU I WAS STRUGGLING TO DO THISS
I have a question. You used your current bit mask layers that you had to know when to stop the ray cast, but I have added another layer and mask layer to my enemy, but the problem is that now if I have multiple enemies chasing me, if one enemy that is tracking my player goes behind another, the line of sight will stop and the enemy in front. How could I specify which mask bits I wanted to detect instead of using all the current mask bits I have selected for my enemy? (In this case I just want the first bit mask and if possible, the second as well)
Can we draw 3d lines draw_line()?
I may be commenting a little bit too late here, but does the turret need to be a kinematic body, or could it be static?
Trying to use this on a kinematicbody but got an error
"Invalid get index 'position' (on base: 'KinematicBody')."
You need a KinematicBody2D
Great tutorial! Do you know how you would get the corners of a 3D object?
Depends on the collision shape. If you're using a box shape, you can get the extents in the same way as the rectangle.
Whenever I place my enemy and player character under a Ysort node the detection for the enemy npc doesn't work at all (no lines being drawn). Although if I move it oustide of the Ysort node everything works fine.
Keep it up man. Once I start making money, I'll donate brother. But until then thank you
Could you do a tutorial on how to cast shapes? For example, Unity 3D has a kind of raycast called, "SphereCast." It casts a sphere wherever it's pointing. This is perfect if you want to make your own character controller.
Is there a way, to make a Circular Raycast, or make a Raycast do an instant 360? Because im trying to make a Granade and it would be really helpful for the explosion
I'd use an Area2D or `Physics2DDirectSpaceState.intersect_shape()`
When I try to run it, it says : invalid index 'shape' (on_base: 'null instance') with value of type 'CircleShape2D'.
That happens due to files being strange or due to corruption, try restarting your computer or restarting Godot
thanks for the tutorial, that was amazing
If it is possible, maybe do a video about a pop up option menu on mobile devices that appears over any level which uses a minimum amount of cpu when it is not needed.
or you could just create a custom collision bounding box shape one for detect and another for shoot. no rays. no masks. a lot less code (but wouldn't be able to use in different environments without hand making a new detect collision box shape for each placement if positioning to obstacles is different ) just thought be good to mention alternative pro and con.
quick question, how do you give name to collision level?
Project Settings -> Layer Names -> 2D Physics
can't we do all of this by creating a raycast2d node?
i get : Script inherits from native type 'CollisionShape2D' so it can't be instanced in object of type 'Area2D' when i run my game now :(
You seem to have taken a script that starts with "extends CollisionShape2D" and attached it to an Area2D. A script attached to an Area2D must start with "extends Area2D".
@@Kidscancode yeah exact ty man 😋
Great explanation. thanks
Cool tutorial, thanks for that!
I've a question, how is it possible to make the enemy shoots at you when he only sees you.
Hi Chris! You are awesome!! Could you please make tutorial on creating a top down tile based game in godot 3 (like that you have made for pygame or something new)?
Hello do you any tutorial for light2d? I know the basic but I would like to learn how to do nice light effects
Yes, Light2D would be a good one. Have you looked at the code from the official demos?
no, not yet. I'm going to see it later thanks.
how did you make the bullets fly out? I do not even have the mob to spin, although I did everything like you
Did you connect the shoot signal?
Can you do this but instead of shooting, have the turrets chase you and melee. I would like to learn how to do that. Great video man as always.
Sure. In my example, I'm using the direction of the raycast to set the direction of the bullets. You could just as well use that direction vector to set your mob's velocity.
Hey men great tutorial, i search on internet and didn't found how to properly set up the collision mask and i end up with my result.collider.name always returning "EnnemyArea" wich is an area for making my ennemy collinding with a bunch of stuff, my raycast seems to hit this area first.
I think if i set up correctly my layerMask it should work but i don't find any tutorial for godot 3 may you help me ?
docs.godotengine.org/en/latest/tutorials/physics/physics_introduction.html#collision-layers-and-masks
Thanks man, i got it work with exclude everything in the intersect_ray() method.
If i understand correctly i have now two layers name player and ennemy, on my player kinematicBody i set the layer to Player and the mask to Ennemy and the opposite for the Ennemy kinematic body and it will work great ?
"Indentifier not found: pos" in shoot(pos) i dont understand why :( Can someone help??
That means you haven't declared a variable named "pos". Perhaps you made a typo?
i could not get the result.collider.name to be compared with the player object.
is there any solution to it..
i used player_node = get_parent().get_node("Player")
could not compare if result.collider.name == player_node or "Player" or player_node.get_node("PlayerBody") #its collision name
could u help me out ASAP..
Thanks in advance..
What does "could not get it to be compared" mean? Did you get an error message? Was your code identical to the code in the tutorial? I can't suggest a solution without knowing what the problem is.
How would I go about scaling this down to 32x32 tiles? I have tried but the raycast messes up and hits walls before it actually collides with them.
Tile size should not matter. However, you can't just scale a TileMap - you have to use 32x32 tiles.
@@Kidscancode I changed the tilemap cell size to 32 along with editing the icon.png down to 32x32 and fixing all of the scalings of the turret and player but now the ray is stopped by some invisible collider I can't find. Here is what I am seeing: imgur.com/VwBnBEr
The TileSet itself needs to be rebuilt with 32x32 tiles and collisions. You're using tiles with 64x64 collisions on a 32x32 map.
@@Kidscancode I had to reimport the icon.png to get it to see that it was 32x32 and fixed the shape offset. I am very new to this engine and am trying to learn stuff as I need it.
thank for the best video ever in your channal i like it THAKYOU
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
^^
i love this channel
The thing is i only wanted to do shooting (by main player) and i only use some of your code, and i didn't see var/const pos, so i think it is declared in some other way, dont know where
It's a loop variable. "for pos in..."
Oh, thanks
ur the best 👍
Nice tutorials mate. Got one issue, when i'm using
narrow rectangle, it's doesnt work properly.
I don't really understand your question. What do you mean by "properly"? What goes wrong? What rectangle is narrow?
@@Kidscancode I'm trying to use your concept in my game. Your project works just fine. But in my case i can't get full rotation, it's only works from the left side of the screen. Then i took your code, with all signals, masks... and got the same result. Trying to figure out what is going wrong. Narrow rectangle is like 10 by 70
@@Kidscancode Figured out what was wrong. Offset. Just forgot about it. Your code works perfectly! Thanks
Great tutorial, thanks a lot! :) Question: Do you think we can use raycast2d aswell for something else, like doing a wall jump for example?
Certainly. Raycasts have lots of uses. That's one common use of a raycast, although a shape might work better to detect touching a wall. There is also "is_on_wall()".
@@Kidscancode Oh great, thanks you very much! I thought it was possible but I was kinda unsure about it! :)
Please Make more videos ?
I'm working on it. Any requests?
@@Kidscancode please make a rocket that follows you and explodes upon contact.
Please !
I wanna participate in my first game jam.
@@Kidscancode smooth following rocket. With explosion particles.
I did that in my tank project: ua-cam.com/video/KAUP4nlJyz0/v-deo.html
This is awesome
Man u genius, thanks a lot.
Hey man.
Really thanks for all your tuts. They're insanely good.
But for this one it's a bit different. It's good and all, but I'd like to know how it works. So I wanted to reverse-engineered your script.
I made a blank project, setup a new character who have different properties than yours.
I exported the turrets from your projet and tried to tweak it to make them able to recognize my character... But couldn't make it happen. The turrets don't target this new character of mine and I can't figure out why.
For testing purpose I tried to import the character from the ray-casting project to my own... The turrets are able to aim him but do not shoot anything.
Is that because importing things from a project to another is broken, or is there specific things in your Main scene or Player that make it works ?
I'm going to make turrets step by step after that. But I wanted to try to tweak it a little before trying to code it by myself because I'm a complete beginner with Godot...
Once again, thanks for your great tutorials.
Most likely you didn't set up your collision layers like I did, and that's why the turrets don't see the player.
This really wasn't intended as a beginner-level demo, which is why I didn't go too deeply into the initial setup. It was specifically to talk about the raycasting technique as applied to a common problem.
Oh, I see. That's pretty instructive anyway.
Going to look at it in depth. And... Thanks for your answer btw. :)
this is gold!
please make raycast3d tutorial :(
First off thank you very much for taking the time to make these videos. Is this compatible with 3.1? I got to the section about adding the directions and received and error on this line:
var ne = target.position + Vector2(target_extents.x - target_extents.y)
The error reads as follows:
error(34,29): No constructor of 'Vector2' matches the signature 'Vector2(var)'.
If anyone reads this and/or replies I appreciate it.
You're missing the comma between the two numbers in your vector2. The y is supposed to be negative, not taken away from the x
Thank you very much for your response. I've been staring at this code for far too long. Hey so on line 27 he has the following line:
var target_extents = target.get_node('CollisionShape2D').shape.extents - Vector2(5, 5)
Would this still work with a CollisionPolygon2D?
@@nailbomb420
@@OsamaBombLyrics No it wont work with a polygon, it's specific to the rectangleshape2d, i.e they're different properties for the different shapes, circleshape2d has radius instead for example.
If you used polygon for no real reason, just swap it to a rectangleshape2d, if the polygone is fairly important to the shape of your character, then consider adding an area2d with a rectangleshape and using that for basic stuff like this.
When it comes to most collision you will find it much much easier to use rectangles or circles, as the maths is quite simple to figure out intersections. Ass soon as you start to rotate things or use odd shapes it means either alot more more work, or higher level of maths :/ (which is annoying) So I suggest using rectangles where possible. Note, that you can make the rectangle much smaller than the size of the sprite, i.e many 32x32 sprites can have rectangleshape colliders 16x16 or some other dimensions.
Hope this helps.
@@OsamaBombLyrics here's a link to the polygon2d class documentation: docs.godotengine.org/en/3.1/classes/class_polygon2d.html
the polygon does store all the points that define it, so you could in theory get that list of points and use them instead of the rectangleshapes extents, as they're both just points in space. Might be worth checking out.
@@nailbomb420 Yessss! I got it working! Thanks for the tip and responses! I just used another CollsionShape2D on the Player with its roll to specifically handle interacting with the Turret. I will probably take your advice and switch my CollsionShape2D's to either rectangles or circles. I was using the Polygon2D shapes because I wanted all the collisions to be as exact as I could make them. However, if it will be just a bigger headache in the long run I will just keep things simple. I'd rather have a working game then nothing at all.
you're the best
Hi. How could i contact you? I need some help with python.
There's a link on my website to send me email. Or you can post here in the YT comments if it's a relatively short one. I can't promise I can help you, but I'll take a look.
great tutorial ! I was wondering will you do python again ?
Sooner or later. There's lots of Python material out there already, and not as much for Godot, and I've gotten very involved with the project so it's my focus right now.
Amazing!!!!
Awesome!
ty
More, give us more
красава !)))))))
Bad language choice,