Godot 3.0: 2D Visibility with Ray-casting

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

КОМЕНТАРІ • 124

  • @hamzahichmine7084
    @hamzahichmine7084 11 місяців тому +2

    For all these years still this code help people

  • @eljoagames
    @eljoagames 6 років тому +58

    You are like a hero, the hero of Godot and all you teach is a gift for us. Thank you a lot!!

  • @josesena4314
    @josesena4314 6 років тому +32

    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!

  • @benrainero9796
    @benrainero9796 4 роки тому +1

    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

  • @daviddalisay5317
    @daviddalisay5317 5 років тому +2

    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.

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

    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!

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

    The tutorial is still very helpful after all the years, thank you.

  • @noc2_art
    @noc2_art 5 років тому +3

    What a master! And you devise such effective and scalable solutions. Thank you so much for sharing your knowledge with us.

  • @MisterBrown94
    @MisterBrown94 6 років тому +1

    Every video is soooo useful. No matter what you cover, I always learn something I didn't know. Thank you a lot!

  • @rafaelgpontes
    @rafaelgpontes 6 років тому +1

    Man, your tutorials are great! Be sure of one thing: you are changing lives, man!!! Keep up the great work. Cheers from Brazil. :)

  • @androskris
    @androskris 6 років тому +3

    Nice, wish I had tutorials like this when I was 15 and trying to code games on my C64 with Gary Kitchen's Gamemaker

  • @indigoblacksteel1176
    @indigoblacksteel1176 6 років тому +3

    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.

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

    Thank you so much! Probably the best godot tutorials I have ever seen!

  • @MsKocagoz
    @MsKocagoz 6 років тому +4

    One more great tutorial, with your help I will open my own game studio :)

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

    Added another tool in my arsenal as a game dev. Thanks for this!

  • @wearegeek
    @wearegeek 6 років тому +1

    Awesome! One can never have enough Godot tutorials. ^_^

  • @thomashalsted1888
    @thomashalsted1888 6 років тому +1

    I needed this video a lot!
    I'm making a player-controlled turret that fires LASERS. It's fun.

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

    great tutorial with clear explation of every code

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

    Excellent tutorial and lesson !!!

  • @RexN6
    @RexN6 6 років тому +1

    Hey! I was looking for something like this!! Thank you very much!

  • @fossegutten6579
    @fossegutten6579 6 років тому +4

    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?

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

      In Project Settings -> Layer Names -> 2d Physics

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

    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

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

    Can you change the circles to a Sector instead?????? Pliz tel me

  • @seishumorales9758
    @seishumorales9758 5 років тому +1

    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.

  • @xy0157
    @xy0157 6 років тому +1

    great tutorials! also, writing in comments would be good for reinforcement

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

    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!

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

    Really nice stuff, thanks for this!

  • @EightNineOne
    @EightNineOne 6 років тому

    This is immensely useful. Thank you so very much.

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

    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.

  • @FeniksGaming
    @FeniksGaming 6 років тому

    Great video as always

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

    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

  • @JAAAA09383-Y
    @JAAAA09383-Y 3 роки тому

    THANK YOU I WAS STRUGGLING TO DO THISS

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

    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)

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

    Can we draw 3d lines draw_line()?

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

    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?

  • @ejoojoo
    @ejoojoo 4 роки тому +1

    Trying to use this on a kinematicbody but got an error
    "Invalid get index 'position' (on base: 'KinematicBody')."

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

    Great tutorial! Do you know how you would get the corners of a 3D object?

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

      Depends on the collision shape. If you're using a box shape, you can get the extents in the same way as the rectangle.

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

    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.

  • @joshdoesthings7836
    @joshdoesthings7836 6 років тому +1

    Keep it up man. Once I start making money, I'll donate brother. But until then thank you

  • @OmegaF77
    @OmegaF77 6 років тому

    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.

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

    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

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

      I'd use an Area2D or `Physics2DDirectSpaceState.intersect_shape()`

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

    When I try to run it, it says : invalid index 'shape' (on_base: 'null instance') with value of type 'CircleShape2D'.

    • @t.h.mgaming4128
      @t.h.mgaming4128 3 роки тому

      That happens due to files being strange or due to corruption, try restarting your computer or restarting Godot

  • @3DSNC
    @3DSNC 4 роки тому

    thanks for the tutorial, that was amazing

  • @memzen8101
    @memzen8101 6 років тому

    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.

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

    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.

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

    quick question, how do you give name to collision level?

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

      Project Settings -> Layer Names -> 2D Physics

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

    can't we do all of this by creating a raycast2d node?

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

    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 :(

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

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

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

      @@Kidscancode yeah exact ty man 😋

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

    Great explanation. thanks

  • @mohdseraj1047
    @mohdseraj1047 6 років тому

    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.

  • @wasimkabir4398
    @wasimkabir4398 6 років тому

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

  • @cesarfernandez2368
    @cesarfernandez2368 6 років тому

    Hello do you any tutorial for light2d? I know the basic but I would like to learn how to do nice light effects

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

      Yes, Light2D would be a good one. Have you looked at the code from the official demos?

    • @cesarfernandez2368
      @cesarfernandez2368 6 років тому

      no, not yet. I'm going to see it later thanks.

  • @askaranarbekov3145
    @askaranarbekov3145 6 років тому

    how did you make the bullets fly out? I do not even have the mob to spin, although I did everything like you

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

      Did you connect the shoot signal?

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

    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.

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

      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.

  • @tomandrieu3876
    @tomandrieu3876 6 років тому

    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 ?

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

      docs.godotengine.org/en/latest/tutorials/physics/physics_introduction.html#collision-layers-and-masks

    • @tomandrieu3876
      @tomandrieu3876 6 років тому

      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 ?

  • @aleksanderzajkowski8285
    @aleksanderzajkowski8285 6 років тому +1

    "Indentifier not found: pos" in shoot(pos) i dont understand why :( Can someone help??

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

      That means you haven't declared a variable named "pos". Perhaps you made a typo?

  • @muthukrish1318
    @muthukrish1318 6 років тому

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

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

      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.

  • @av088r
    @av088r 6 років тому

    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.

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

      Tile size should not matter. However, you can't just scale a TileMap - you have to use 32x32 tiles.

    • @av088r
      @av088r 6 років тому

      @@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
      @Kidscancode  6 років тому

      The TileSet itself needs to be rebuilt with 32x32 tiles and collisions. You're using tiles with 64x64 collisions on a 32x32 map.

    • @av088r
      @av088r 6 років тому

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

  • @Romeoill
    @Romeoill 6 років тому +3

    thank for the best video ever in your channal i like it THAKYOU
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    ^^
    i love this channel

  • @aleksanderzajkowski8285
    @aleksanderzajkowski8285 6 років тому

    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

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

    ur the best 👍

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

    Nice tutorials mate. Got one issue, when i'm using
    narrow rectangle, it's doesnt work properly.

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

      I don't really understand your question. What do you mean by "properly"? What goes wrong? What rectangle is narrow?

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

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

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

      @@Kidscancode Figured out what was wrong. Offset. Just forgot about it. Your code works perfectly! Thanks

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

    Great tutorial, thanks a lot! :) Question: Do you think we can use raycast2d aswell for something else, like doing a wall jump for example?

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

      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()".

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

      @@Kidscancode Oh great, thanks you very much! I thought it was possible but I was kinda unsure about it! :)

  • @froyorex4856
    @froyorex4856 5 років тому +1

    Please Make more videos ?

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

      I'm working on it. Any requests?

    • @froyorex4856
      @froyorex4856 5 років тому +1

      @@Kidscancode please make a rocket that follows you and explodes upon contact.
      Please !
      I wanna participate in my first game jam.

    • @froyorex4856
      @froyorex4856 5 років тому +1

      @@Kidscancode smooth following rocket. With explosion particles.

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

      I did that in my tank project: ua-cam.com/video/KAUP4nlJyz0/v-deo.html

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

    This is awesome

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

    Man u genius, thanks a lot.

  • @Condetg
    @Condetg 6 років тому

    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.

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

      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.

    • @Condetg
      @Condetg 6 років тому

      Oh, I see. That's pretty instructive anyway.
      Going to look at it in depth. And... Thanks for your answer btw. :)

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

    this is gold!

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

    please make raycast3d tutorial :(

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

    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.

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

      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

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

      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

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

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

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

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

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

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

  • @avvvqvvv99
    @avvvqvvv99 6 років тому +1

    you're the best

  • @raremc1620
    @raremc1620 6 років тому

    Hi. How could i contact you? I need some help with python.

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

      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.

  • @TheFAA213
    @TheFAA213 6 років тому

    great tutorial ! I was wondering will you do python again ?

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

      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.

  • @NahomyUchija
    @NahomyUchija 6 років тому

    Amazing!!!!

  • @Pspisripoff
    @Pspisripoff 6 років тому

    Awesome!

  • @ryzhucks4624
    @ryzhucks4624 6 років тому

    ty

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

    More, give us more

  • @романярошенко-ц8ъ
    @романярошенко-ц8ъ 6 років тому

    красава !)))))))

  • @truelion-rt6gd
    @truelion-rt6gd 5 років тому

    Bad language choice,