Thank you for all the tutorials! I started learning godot few days ago and I've been binge watching your stuff. Very informative tutorials, and you also have a pleasant voice to listen to. While programming my first 2D platformer, I found a great way to use raycasts for enemy AI. I wanted the enemy to patrol on one platform so, that it wouldn't run off the edge. I put raycast in front of the enemy, and as soon as it doesn't detect ground anymore, enemy changes direction.
Thank you so much. Some of this went over my head, but I was able to understand just enough to get the RayCast2D to do what I want it to in my prototype. You're the best!!
if the player is standing at the very edge of the platform, isn't it possible that the player is still colliding with the platform and standing on it but we can't jump because the ray cast doesn't collide anymore? what to do then?
In that case you have a decision to make. There are a lot of different ways to make platform physics, and they vary a lot. For example, some games have 2 raycasts pointing down - one at each edge of the player.
Could you do a tutorial on why and how do you use those variables in func.. like "func do_something(thing1, thing2)? I still dont get them, like, you make these variables there or.. and how program understands that they wield some sort of information, when its not been previously mentioned before Ready section....?
Using functions is more of a basic programming topic, not specific to Godot. When you define a function, you specify what arguments it requires, like so: func area(radius): return PI * radius**2 Now, if I want to use the area function, I have to pass it a value in the parentheses, which it will use as the radius in the function. This might help you: automatetheboringstuff.com/chapter3/
In the second example, if you are already checking for collisions of the player to the ground, at least from the performance point of view, isn't it better to allow the jump based on the collision, rather than based on the ray cast?
check_collision() only tells us the player hit something, it doesn't tell us it was the ground. Using the raycast we can be sure we're standing on the ground and allow jumping. Don't worry about performance unless performance is a problem.
Yes, but then you also have to check if the colliding object is below you. Raycasts are pretty efficient and easy to work with, that's the idea behind this technique. There are others - for example, adding a separate collisionshape just at the feet of the character.
What I worry about the raycasts is that it seams that performance would be worse(but probably not noticeably worse) because direction matters, while with a simple shape it would only matter if it's colliding or not.
That's a false assumption. First of all, never worry about performance if you don't have an actual, measurable performance problem - it's a waste of time that would be better spent on building your game. You even said "not noticeably worse" - if it can't be noticed, how can it be a problem at all? What you're doing is called "premature optimization" and it's a trap that programmers fall into all the time - worrying about a theoretical future performance need that may never even appear. Meanwhile you consume valuable development time coming up with a solution that will never be needed. Second, remember even with a "simple shape" - and a ray is simpler than any other shape, for that matter - you're not consuming CPU only when colliding, because it is continually testing for collisions. Trust me: don't worry about it. :)
This video is fairly old by now, but I’m wondering what your opinion is on using a RayCast2D to detect when the player is on the ground vs using the is_on_floor() method after calling move_and_slide(). Obviously I won’t always be using move_and_slide(), so I could use RayCasts for those cases, but are there any advantages to using a RayCast over an is_on_floor() if I’m already using move_and_slide()?
Correct me if I am wrong, and I might be since I am just learning, but isn't this more expensive then just requesting the normal of the collision? If it is pointing up then you are on the ground, if not then you are colliding with a wall. You can even remove that check and just jump at an angle equal to half the normal and a verticle vector. This means 45 degrees off a verticle wall, 90 degrees off the ground and any amount in between for slanted walls. You would need to ignore any vectors with positive Y component to prevent ceiling jumps. Like I said, just learning and mostly I want to know if my thought is wrong. Thanks for the video.
7 років тому+2
in the description there is a error : This time we'll be demonstrating how to use the PathFollow2D node should be : RayCast2D
Raycast is actually bad for detecting ground, cuz once its even few pixels out of smt, it says there is nothing... Like when you have two square boxes,, 5pix or more apart, and when player steps on the middle of them, leaving raycast in air..Youre stuck. I found adding extra collision shapes(like a small circle or square) works pretty well in that regard.. But im beginer in this anyways But about that shooting.... thats very interesting! Thank you for great tuts! :)
A very common setup is to use 2 raycasts - one at each edge of the player - to detect ground. Your solution is also viable, but of course for this demo I was only talking about raycasts.
иногда проверять нужно не персонажа, а какой-то объект. ты не можешь так легко вычислять на полу ли rigid body, если не будешь пользоваться рэйкастом. is_on_floor() работает только вместе с move_and_дополнить методами, а они для rigid bodу не определены. ты можешь напрямую сверять позицию rigid body с позицией пола, но это гемморойно и аще отстой.
Thank you for your tutorials. They have been instrumental in my progress as an aspiring game dev. Regarding the first example, I'm designing a top-down horseback shooter, and I'm trying to get the ray to detect an enemy horseman (KinematicBody2D). The problem is that the enemy's area is completely inside the horse's (also a KinematicBody2D). Is there a way to make the raycast ignore particular objects, or detect them but still travel beyond them to detect further objects? I've tried add_exception based on the collider's group name, and I've also attempted layering to no avail, but it's possible I'm not going about either of these techniques the correct way.
Based on what you've said, I'd think that using the layer system should work fine. Put all the horsemen on a different layer from the horses, and set the mask of the raycast to only work on the horseman layer. There's also a Type Mask, but it sounds like they're both K2D, so that wouldn't help.
Thank you for all the tutorials! I started learning godot few days ago and I've been binge watching your stuff. Very informative tutorials, and you also have a pleasant voice to listen to.
While programming my first 2D platformer, I found a great way to use raycasts for enemy AI. I wanted the enemy to patrol on one platform so, that it wouldn't run off the edge. I put raycast in front of the enemy, and as soon as it doesn't detect ground anymore, enemy changes direction.
Good job! That's an excellent use for raycasts.
That is an amazing idea !
Thank you so much. Some of this went over my head, but I was able to understand just enough to get the RayCast2D to do what I want it to in my prototype. You're the best!!
This series is great! Thanks KidsCanCode.
Thanks! Still helping people like me learn 5 years later :)
These is a really great series! Thank you so much!
You're welcome!
Thank you so much!! I spent a whole weekend googleing the soluction that you give and nobody explained that😅
Why can't you keep the fire button pressed down? I only seem to get a ray collision if I repress the space button. Thanks
man this is so useful, thank you!!
Amazing videos, what a legend.
Is there something like Raycast but instead of a line, it's an area that fans out between two lines following an arc radius or something?
if the player is standing at the very edge of the platform, isn't it possible that the player is still colliding with the platform and standing on it but we can't jump because the ray cast doesn't collide anymore? what to do then?
In that case you have a decision to make. There are a lot of different ways to make platform physics, and they vary a lot. For example, some games have 2 raycasts pointing down - one at each edge of the player.
Could you do a tutorial on why and how do you use those variables in func.. like "func do_something(thing1, thing2)?
I still dont get them, like, you make these variables there or.. and how program understands that they wield some sort of information, when its not been previously mentioned before Ready section....?
Using functions is more of a basic programming topic, not specific to Godot. When you define a function, you specify what arguments it requires, like so:
func area(radius):
return PI * radius**2
Now, if I want to use the area function, I have to pass it a value in the parentheses, which it will use as the radius in the function. This might help you: automatetheboringstuff.com/chapter3/
How did you make the trail?
Why not using is_on_floor function?
Because that function didn't exist when this video was made.
In the second example, if you are already checking for collisions of the player to the ground, at least from the performance point of view, isn't it better to allow the jump based on the collision, rather than based on the ray cast?
check_collision() only tells us the player hit something, it doesn't tell us it was the ground. Using the raycast we can be sure we're standing on the ground and allow jumping. Don't worry about performance unless performance is a problem.
But you can use 'get_collider()' when 'is_colliding()' is true. The problem is that 'get_collider()' returns only one object.
Yes, but then you also have to check if the colliding object is below you. Raycasts are pretty efficient and easy to work with, that's the idea behind this technique. There are others - for example, adding a separate collisionshape just at the feet of the character.
What I worry about the raycasts is that it seams that performance would be worse(but probably not noticeably worse) because direction matters, while with a simple shape it would only matter if it's colliding or not.
That's a false assumption. First of all, never worry about performance if you don't have an actual, measurable performance problem - it's a waste of time that would be better spent on building your game. You even said "not noticeably worse" - if it can't be noticed, how can it be a problem at all? What you're doing is called "premature optimization" and it's a trap that programmers fall into all the time - worrying about a theoretical future performance need that may never even appear. Meanwhile you consume valuable development time coming up with a solution that will never be needed.
Second, remember even with a "simple shape" - and a ray is simpler than any other shape, for that matter - you're not consuming CPU only when colliding, because it is continually testing for collisions. Trust me: don't worry about it. :)
This video is fairly old by now, but I’m wondering what your opinion is on using a RayCast2D to detect when the player is on the ground vs using the is_on_floor() method after calling move_and_slide(). Obviously I won’t always be using move_and_slide(), so I could use RayCasts for those cases, but are there any advantages to using a RayCast over an is_on_floor() if I’m already using move_and_slide()?
is_on_floor() works well enough for most cases. I wouldn't use a raycast for floor detection if I were using a kinematic body.
Correct me if I am wrong, and I might be since I am just learning, but isn't this more expensive then just requesting the normal of the collision? If it is pointing up then you are on the ground, if not then you are colliding with a wall.
You can even remove that check and just jump at an angle equal to half the normal and a verticle vector. This means 45 degrees off a verticle wall, 90 degrees off the ground and any amount in between for slanted walls. You would need to ignore any vectors with positive Y component to prevent ceiling jumps.
Like I said, just learning and mostly I want to know if my thought is wrong. Thanks for the video.
in the description there is a error : This time we'll be demonstrating how to use the PathFollow2D node should be : RayCast2D
Thanks!
Is the information in this video still relevant after more than 4 years or the API has changed?
Raycast is actually bad for detecting ground, cuz once its even few pixels out of smt, it says there is nothing... Like when you have two square boxes,, 5pix or more apart, and when player steps on the middle of them, leaving raycast in air..Youre stuck. I found adding extra collision shapes(like a small circle or square) works pretty well in that regard.. But im beginer in this anyways
But about that shooting.... thats very interesting! Thank you for great tuts! :)
A very common setup is to use 2 raycasts - one at each edge of the player - to detect ground. Your solution is also viable, but of course for this demo I was only talking about raycasts.
I have the same problem, could you please make a tutorial on how to make good ground detection?
@@PiterTraceurFA: Just use "if is_on_floor..."
ty sir
9:41 Platform raycast tutorial starts here.
set_pos change to set_position
Пример с платформером -- плохой пример, т.к. лучше сделать переменную состояния, которая будет показывать в прыжке персонаж или нет.
иногда проверять нужно не персонажа, а какой-то объект.
ты не можешь так легко вычислять на полу ли rigid body, если не будешь пользоваться рэйкастом. is_on_floor() работает только вместе с move_and_дополнить методами, а они для rigid bodу не определены. ты можешь напрямую сверять позицию rigid body с позицией пола, но это гемморойно и аще отстой.
Thank you for your tutorials. They have been instrumental in my progress as an aspiring game dev.
Regarding the first example, I'm designing a top-down horseback shooter, and I'm trying to get the ray to detect an enemy horseman (KinematicBody2D). The problem is that the enemy's area is completely inside the horse's (also a KinematicBody2D).
Is there a way to make the raycast ignore particular objects, or detect them but still travel beyond them to detect further objects? I've tried add_exception based on the collider's group name, and I've also attempted layering to no avail, but it's possible I'm not going about either of these techniques the correct way.
Based on what you've said, I'd think that using the layer system should work fine. Put all the horsemen on a different layer from the horses, and set the mask of the raycast to only work on the horseman layer. There's also a Type Mask, but it sounds like they're both K2D, so that wouldn't help.
At 3:15 "First of all, any time you add a new RayCast2D it is disabled by default"
*facepalms*
Top that?!
444 likes 4 dislikes
wow
nice