Godot Engine - Know Your Nodes: RayCast2D
Skills:
AI Pair Programming60%
Key Takeaways
Explores the RayCast2D node in Godot Engine, demonstrating its usage and applications
Full Transcript
welcome back to know your nodes this series of good game Engine videos where we take a single node type from the engine and show how it's used in this video we will be talking about the raycast 2D node so what is a raycast if you've done much reading about game development you've probably come across the term because rays and Ray casting are very useful for solving a lot of different problems in games at its most basic array just like you learned in Geometry is a line segment project ing out from a start Point projecting this Ray is called casting and its purpose is to tell you if and where it hits something as you can see the raycast 2D node in gdau has a bunch of different methods having to do with collisions and we're going to look at how to use them as I said there's lots of uses but for this video I'm going to demonstrate two of them ground detection and high-speed projectiles so for this demo I've made a simple top down shooter style scene with a player that can walk around and walls that are static body 2DS so that we have collisions and what we want to do is Implement shooting now one way that you can do this is that you can have uh Sprites that are instance at the tip of the barrel of the gun and travel in a straight line until they hit something but where this breaks down is if you want to have your projectiles be very fast moving if you create a node and animate it moving extremely fast then you're going to have your physics breakdown if you have an object that is relatively narrow and the speed of your projectile is really fast then it's possible that between frames it will move from one side to the next and you won't detect that collision and you have to start doing a lot more expensive calculations to to make sure that you don't um you don't have that tunneling effect which is what it's called so instead what we want to do is say you're firing a bullet from a gun that bullet travels really really fast and all we care about is what does that bullet hit so if I shoot the gun right now it should hit the wall over here and that's where I should see the hit and I don't really care about animating it traveling along that distance because it moves too fast for you to see right it's just going to hit what it hits and that's what we're going to use array cast for we're going to cast array from the gun in a straight line and detect where it hits so here's our player scene we have a kinematic body 2D with a Sprite and a collision shape attached to it and a script that's handling all of the character controls uh rotation and movement and wall collision and so we want to now do the shooting and so we we want to add raycast 2D to this so we're going to go to play we're going to add a raycast 2d and there we go and see it's it's drawn in the editor as a blue arrow and now we just want to change a couple of the parameters first of all anytime you add a new raycast 2D it is disabled by default so if you want to use it you need to make sure to turn it on and of course you can do that in code as well and and first of all we want this to come out of the barrel of the gun not out of the player's forehead so we're going to adjust the position and the barrel of the gun is about 30 in X and 10 in y all right so we're going to put that at the tip of the gun barrel and for cast two which is this parameter right here this is set to 0 comma 50 which is why it's pointing down so we want the R to come out of the gun which is pointing in the X Direction and we're going to put a large number here we're going to put a th and we're going to set the Y to zero and so this Ray is going to come out of the gun and go a long distance because what we want to know is does this hit anything as far as the bullet travels and we can see this in action if we turn on the visible Collision shapes and run our main scene you will see that Ray coming out of the player here right and showing what direction our imaginary bullet is going to travel when we shoot it and so now we just want to query that raycast and see where it hits right and we're going to get this point right here okay so let's go to the script and let's rename this to the shoot Ray and I'm going to copy this as well because what I want to do is in the player script I'm going to load a variable for it shoot Ray is equal to get node shoot Ray so now we have that to refer to and we're going to set process input to true so that we can have whenever we press a the space bar we're going to shoot bullet so on our input method we're going to just say if uh event dot is action pressed UI select which is the space bar by default then we're going to shoot whatever shoot means well let's define that so the shoot function could do a lot of different things but what we really care about is we're going to chck test if the shoot Ray is colliding and if the shoot Ray is colliding what should we do well we need some sort of Vis visual feedback that we've hit something and so on the map you know on the 2D map again on the wall somewhere we want to see a puff of we're going to see a puff of smoke so we can see that the bullet hit there so what we're going to do is we're going to emit a signal so I'm going to Define that signal up here and I'm just going to call it I'm just going to call it hit and then we're going to emit signal hit and oops and then we're going to pass along the location of the hit which is shoot ray. getet Collision point and that's going to be the location of where it hit you can also get Collision normal you can get collider or collider shape if you want to know what thing you hit uh or what part of a thing you hit but we care about the point where we hit because that's where we want to spawn the little puff of smoke okay so that's our player so whenever we hit space we're going to emit that signal if the ray collides with something now to do that I've just made a quick little particle 2D scene that just does a little puff of smoke like this and that's what we want to instance at the hit location so in our main scene in The Script we have our player instance and we're just going to connect that signal so player. connect the signal is hit and the method we want to call is show hit we pass in the hit location and we just instance one of those puffs of smoke at that location and that's going to look like this let's turn off the Collision shapes for a moment so you can see now whenever I press the space bar I get a hit at whatever place on the wall that projectile hit so that's it and so just to add a little bit more visual appeal I'm not going to go into this in too much detail but I just added a quick little Trail so that when we emit the signal we emit the barrel location which is the player's location plus the offset which was 30 comma 10 as well as the Collision point and and in the main script when we get those two locations I am instancing this little this little Trail which is just a an animated Sprite that that Fades and so that just makes us look a little bit nicer like we have a little path where you can see where the bullet went and just this is a quick and dirty one um obviously you can make it look a lot better but for the purposes of this demo just wanted you to be able to see the path of the bullet as well as where it hits I hope that was helpful now let's look at one other application of the raycast 2D and this one is even simpler so we've got a simple platformer here where we have a character and a platform and using the standard kinematic body 2D we get the Collision so the player Falls stands on the platform can run back and forth but we want to do jumping and right now what I have is if the up arrow is pressed we set the velocity to a negative value which makes them go up but the problem with that is I can keep pressing the up Arrow whenever I want and I get a negative velocity every single time and I don't want that I only want the player to be allowed to jump when they're standing on on the ground and that is another perfect use for a raycast 2d in fact we can do a very very simple one so first we open our player scene and we're going to add the raycast 2D now we want this we want this raycast 2D to detect the ground which means we want it to be sticking down below because right now what are we going to get we're going to get a collision constantly because this raycast 2D is colliding with the player and so we want to do a couple things let's name this ground Ray let's enable it always remember to do that we want to cast or sorry first we want to move it let's move it downwards right I want it to come out from the bottom of the player and let's also make it a little bit shorter because it doesn't need doesn't really matter but it doesn't need to stick down that far below we just need it to come below the player's feet to where we want to detect the ground so let's just go into the script and we'll add a reference here to It To The Ground Ray so that we can refer to it in our code and here in the jumping now we're going to say if you press the up Arrow key and ground Ray is colliding so if you're pressing upper key and the ground Ray is colliding will let you set your velocity and that's all we have to do so if we run the scene you will see that if I press the up Arrow I jump but if I it a bunch of times nothing happens when I'm already in the air and that's as easy as it gets now if you have more complicated shapes you sometimes will have problems with your Collision detection happening you know the the ray actually collides with the player and you can solve that in a couple of ways the ray cast 2D has an add exception method which lets you name an object object for it to ignore and so if you put the if you put the player object in there then the the Ray would never detect a collision with that object uh you can also as with any other Collision object in gdau use the layer use the Collision layers and the Collision masks to uh set the the Ray on a different layer and have it ignore the player that way and this is also a good method for doing wall jumps you give the player some rays that are sticking out to the left and right and those can tell you whether you're against a wall or not to allow the player to wall jump that's another really common use of raycasts so that'll do it for this video I hope that you found the examples useful so the source code for this project will be linked in the description below feel free to download it and take a look and play with it yourself and use Rays whenever you can I'll see you in the next video for
Original Description
"Know Your Nodes" is a series of Godot Game Engine videos where we take a single node type and show how it’s used. Maybe you’ve never come across this particular node before, or maybe you were just wondering what it’s for, but hopefully after this you’ll have a new tool you can use making your own games. This time we'll be demonstrating how to use the RayCast2D node.
Sample projects from this video can be found here:
https://github.com/kidscancode/godot_tutorials/releases/tag/Know_Your_Nodes
Support me on Patreon: http://patreon.com/kidscancode
Thanks to https://github.com/djrm/godot-design for title image.
Watch on YouTube ↗
(saves to browser)
Sign in to unlock AI tutor explanation · ⚡30
Playlist
Uploads from KidsCanCode · KidsCanCode · 0 of 60
← Previous
Next →
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
Learning to Code with Python: Lesson 1.1 - What is Programming?
KidsCanCode
Learning to Code with Python: Lesson 1.2 - Drawing with Turtles
KidsCanCode
Learning to Code with Python: Lesson 1.3 - Variables
KidsCanCode
Learning to Code with Python: Lesson 1.4 - Loops (and more turtles!)
KidsCanCode
Learning to Code with Python: Lesson 1.5 - Saving and Running Programs
KidsCanCode
Learning to Code with Python: Lesson 1.6 - Functions
KidsCanCode
Learning to Code with Python: Lesson 1.7 - Input and Conditional Statements
KidsCanCode
Learning to Code with Python: Lesson 1.8 - Number Guessing Game
KidsCanCode
KidsCanCode - Patreon Intro Video
KidsCanCode
Learning to Code with Python: Lesson 1.9 - Rock Paper Scissors Game
KidsCanCode
Learning to Code with Python: Lesson 1.10 - Secret Codes
KidsCanCode
Learning to Code with Python: Lesson 2.1 Creating Computer Graphics
KidsCanCode
Learning to Code with Python: Lesson 2.2 Simple Animation
KidsCanCode
Learning to Code with Python: Lesson 2.3: Animating More Objects
KidsCanCode
Learning to Code with Python: Lesson 2.4: More Fun with Animation
KidsCanCode
Extra: Setting up the Atom Editor for Python
KidsCanCode
Game Development 1-1: Getting Started with Pygame
KidsCanCode
Game Development 1-2: Working with Sprites
KidsCanCode
Game Development 1-3: More About Sprites
KidsCanCode
Pygame Shmup Part 1: Player Sprite and Controls
KidsCanCode
Pygame Shmup Part 2: Enemy Sprites
KidsCanCode
Pygame Shmup Part 3: Collisions (and Bullets!)
KidsCanCode
Pygame Shmup Part 4: Adding Graphics
KidsCanCode
Pygame Shmup Part 5: Improved Collisions
KidsCanCode
Pygame Shmup Part 6: Sprite Animation
KidsCanCode
Pygame Shmup Part 7: Score (and Drawing Text)
KidsCanCode
Pygame Shmup Part 8: Sound and Music
KidsCanCode
Pygame Shmup Part 9: Shields
KidsCanCode
Pygame Shmup Part 10: Explosions
KidsCanCode
Pygame Shmup Part 11: Player Lives
KidsCanCode
Pygame Shmup Part 12: Powerups
KidsCanCode
Pygame Shmup Part 13: Powerups (part 2)
KidsCanCode
Pygame Shmup Part 14: Game Over Screen
KidsCanCode
Pygame Platformer Part 1: Setting Up
KidsCanCode
Pygame Platformer Part 2: Player Movement
KidsCanCode
Pygame Platformer Part 3: Gravity and Platforms
KidsCanCode
Pygame Platformer Part 4: Jumping
KidsCanCode
Pygame Platformer Part 5: Scrolling the Window
KidsCanCode
Pygame Platformer Part 6: Game Over
KidsCanCode
Pygame Platformer Part 7: Splash & End Screens
KidsCanCode
Pygame Platformer Part 8: Saving High Score
KidsCanCode
Pygame Platformer Part 9: Using Spritesheets
KidsCanCode
Pygame Platformer Part 10: Character Animation (part 1)
KidsCanCode
Pygame Platformer Part 11: Character Animation (part 2)
KidsCanCode
Pygame Platformer Part 12: Platform Graphics
KidsCanCode
Pygame Platformer Part 13: Improved Jumping
KidsCanCode
Pygame Platformer Part 14: Sound and Music
KidsCanCode
Pygame Platformer Part 15: Powerups
KidsCanCode
Pygame Platformer Part 16: Enemies
KidsCanCode
Pygame Platformer Part 17: Using Collision Masks
KidsCanCode
Pygame Platformer Part 18: Scrolling Background
KidsCanCode
Pygame Platformer Part 19: Wrapping Up
KidsCanCode
Gamedev In-depth Topics: 4-way vs. 8-way Movement
KidsCanCode
Gamedev In-depth Topics: Time-based vs. Frame-based Movement
KidsCanCode
Gamedev In-depth Topics: Non-integer Movement
KidsCanCode
Tile-based game Part 1: Setting up
KidsCanCode
Tile-based game Part 2: Collisions and Tilemap
KidsCanCode
Tile-based game Part 3: Smooth Movement
KidsCanCode
Tile-based game Part 4: Scrolling Map / Camera
KidsCanCode
Tile-based game Part 5: Player Graphics
KidsCanCode
More on: AI Pair Programming
View skill →Related Reads
📰
📰
📰
📰
How I use python to save hours every week
Dev.to AI
What Are AI Software Solutions and How Can They Transform Your Business?
Dev.to · upwork floating infotech
AI Shorts generators you can actually edit (not a black box)
Dev.to · Reel Mint
I Wanted To Automate Website Testing Without Writing Hundreds Of Scripts. AI Changed The Approach.
Medium · AI
🎓
Tutor Explanation
DeepCamp AI