Godot Engine - Know Your Nodes: RayCast2D

Поділитися
Вставка
  • Опубліковано 21 січ 2025

КОМЕНТАРІ • 46

  • @ThomasPlayPiano
    @ThomasPlayPiano 7 років тому +21

    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.

    • @Kidscancode
      @Kidscancode  7 років тому +7

      Good job! That's an excellent use for raycasts.

    • @Hybban
      @Hybban 7 років тому +3

      That is an amazing idea !

  • @vampbat
    @vampbat 3 роки тому +1

    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!!

  • @StewartWebb
    @StewartWebb 7 років тому +12

    This series is great! Thanks KidsCanCode.

  • @milkmadetea
    @milkmadetea 2 роки тому

    Thanks! Still helping people like me learn 5 years later :)

  • @aldowando
    @aldowando 7 років тому +7

    These is a really great series! Thank you so much!

  • @GIFOYT
    @GIFOYT 6 років тому +5

    Thank you so much!! I spent a whole weekend googleing the soluction that you give and nobody explained that😅

  • @stephenmackenzie9016
    @stephenmackenzie9016 2 роки тому

    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

  • @pand2aren
    @pand2aren 7 років тому +1

    man this is so useful, thank you!!

  • @f.r8580
    @f.r8580 4 роки тому

    Amazing videos, what a legend.

  • @xyonblade
    @xyonblade 4 роки тому

    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?

  • @ichigoboy86
    @ichigoboy86 7 років тому +1

    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?

    • @Kidscancode
      @Kidscancode  7 років тому +2

      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.

  • @Pspisripoff
    @Pspisripoff 7 років тому +1

    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....?

    • @Kidscancode
      @Kidscancode  7 років тому +1

      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/

  • @addylenwest8536
    @addylenwest8536 4 роки тому

    How did you make the trail?

  • @MaximoPower2024
    @MaximoPower2024 4 роки тому

    Why not using is_on_floor function?

    • @Kidscancode
      @Kidscancode  4 роки тому +2

      Because that function didn't exist when this video was made.

  • @anakimluke
    @anakimluke 7 років тому

    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?

    • @Kidscancode
      @Kidscancode  7 років тому

      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.

    • @anakimluke
      @anakimluke 7 років тому

      But you can use 'get_collider()' when 'is_colliding()' is true. The problem is that 'get_collider()' returns only one object.

    • @Kidscancode
      @Kidscancode  7 років тому

      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.

    • @anakimluke
      @anakimluke 7 років тому

      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.

    • @Kidscancode
      @Kidscancode  7 років тому +3

      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. :)

  • @unnamedtoaster
    @unnamedtoaster 4 роки тому

    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()?

    • @Kidscancode
      @Kidscancode  4 роки тому

      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.

  • @Critterfluffy1
    @Critterfluffy1 5 років тому

    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

  • @switch3
    @switch3 3 роки тому

    Is the information in this video still relevant after more than 4 years or the API has changed?

  • @Pspisripoff
    @Pspisripoff 7 років тому +1

    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! :)

    • @Kidscancode
      @Kidscancode  7 років тому +1

      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.

    • @PiterTraceurFA
      @PiterTraceurFA 7 років тому

      I have the same problem, could you please make a tutorial on how to make good ground detection?

    • @evanyoung9714
      @evanyoung9714 6 років тому +2

      @@PiterTraceurFA: Just use "if is_on_floor..."

  • @ccgb92
    @ccgb92 7 років тому +1

    ty sir

  • @MedK001
    @MedK001 4 роки тому +2

    9:41 Platform raycast tutorial starts here.

  • @rodrigocorrea9838
    @rodrigocorrea9838 2 роки тому

    set_pos change to set_position

  • @СергейТакой-то
    @СергейТакой-то 4 роки тому

    Пример с платформером -- плохой пример, т.к. лучше сделать переменную состояния, которая будет показывать в прыжке персонаж или нет.

    • @user-lq1jn5nd4d
      @user-lq1jn5nd4d 4 роки тому

      иногда проверять нужно не персонажа, а какой-то объект.
      ты не можешь так легко вычислять на полу ли rigid body, если не будешь пользоваться рэйкастом. is_on_floor() работает только вместе с move_and_дополнить методами, а они для rigid bodу не определены. ты можешь напрямую сверять позицию rigid body с позицией пола, но это гемморойно и аще отстой.

  • @NoReallyTheBestEver
    @NoReallyTheBestEver 7 років тому +1

    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.

    • @Kidscancode
      @Kidscancode  7 років тому +1

      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.

  • @crakbac
    @crakbac 3 роки тому

    At 3:15 "First of all, any time you add a new RayCast2D it is disabled by default"
    *facepalms*

  • @classicguy7813
    @classicguy7813 4 роки тому

    Top that?!

  • @sian2005
    @sian2005 4 роки тому

    444 likes 4 dislikes
    wow
    nice