Projectile and RayCast2D in Godot 4

Поділитися
Вставка
  • Опубліковано 7 жов 2024
  • Projectile / Bullet mechanics in Godot 4 using Ray Cast where it shoots only when it detects the player.
    Code -
    Player - pastebin.com/X...
    Bullet / Projectile - pastebin.com/y...
    Turret / Gun - pastebin.com/5...
    Music Used -
    Hoogway - Found You
    Provided by Lofi Records
    Watch: • Hoogway - Found You
    Download/Stream: fanlink.to/Equ...

КОМЕНТАРІ • 50

  • @tPlayerioT
    @tPlayerioT 11 місяців тому +6

    bruh took me 10min to find whatever why the sprite was not looking at player, then i come to this video and find out that i just have to put "ToLocal". Now i ask why is it a thing, it does work now. Thanks

    • @nolanbudzien2466
      @nolanbudzien2466 10 місяців тому

      Where do you put ToLocal?

    • @tPlayerioT
      @tPlayerioT 9 місяців тому

      ​​​@@nolanbudzien2466ToLocal converts global to local. its a method, you use it first to convert a global position to local, this might solve issues if you have any. 3:03 Most likely to rays.
      youtube didnt notifiy me of that comment

  • @4thdoctor4thewin
    @4thdoctor4thewin Рік тому +1

    Thanks for more amazing content

  • @eloymoraes4756
    @eloymoraes4756 26 днів тому

    thanks man from brazil

  • @leo358
    @leo358 10 місяців тому +1

    Any possible reason why I would be getting this error?:
    - Invalid set index 'direction' (on base: 'Area2D (EnemyBullet.gd)') with value of type 'Vector2'.
    When I edit out this line of code, the combined scene is able to play:
    # ENEMY_BULLET.direction = (RAY_CAST.target_position).normalized()

  • @Maanjiro_g
    @Maanjiro_g 5 місяців тому +1

    is there a way to add range for ray? or do we have to create a collision to detect player for making a range?

    • @inyoke1241
      @inyoke1241 2 місяці тому

      I add a comment in paste bin url with the modification for limit the range to 250px

  • @Fixed_Venus
    @Fixed_Venus 10 годин тому

    Its easier thank i thought thanks

  • @iivan8730
    @iivan8730 8 місяців тому +2

    everything works except this:
    Parse Error: Cannot call non-static function "instantiate()" on the class "PackedScene" directly. Make an instance instead.
    it says this right at the end when i try to instantiate the bullet
    (btw i am using a character body 2D for the turret)

    • @Eudesenho-og3jl
      @Eudesenho-og3jl 4 місяці тому

      voce conseguiu resolver isso ?

    • @nauvalteaz4032
      @nauvalteaz4032 Годину тому

      @@Eudesenho-og3jl make a variable first for instantiate the bullet

  • @peterungar-u8r
    @peterungar-u8r Рік тому +1

    I keep getting this syntax: Invalid get index 'position' (on base: "null instance"). Do you know what could be happening?

    • @16bitdev
      @16bitdev  Рік тому +1

      did u drag and drop the bullet scene into the export variable? if the export variable is empty, then it throws this error

    • @peterungar-u8r
      @peterungar-u8r Рік тому

      Thankyou, yes that helped!@@16bitdev

  • @inyoke1241
    @inyoke1241 2 місяці тому

    Nice work. Stills working in 4.2.2 :)

  • @vEzly
    @vEzly Рік тому

    thank you so much for the video🧡. can you make a video or explain me how I can add a range in which the turret will see the player? sorry I just started to learn gamedev

    • @16bitdev
      @16bitdev  Рік тому

      add Area2D to that turret, inside it add CollisionShape2D, give it a circular shape with desired radius, if a player enters that area turn on the turret (using body entered signal) and start shooting, if the player leaves then turn it off (using body exited signal)

    • @vEzly
      @vEzly 11 місяців тому

      did all that, but how I turn if off? in the shoot func or where? @@16bitdev

    • @leonardodamichi5939
      @leonardodamichi5939 10 місяців тому

      ​@@16bitdevExcuse me, how can I activate the turret when entering the 2D area?

  • @GGAMER232
    @GGAMER232 2 місяці тому

    When I start my game I get this error:
    Invalid get index 'position' (on base: 'null instance').

    • @qvell
      @qvell 2 місяці тому

      me too

    • @Rali-e8b
      @Rali-e8b 2 місяці тому

      @@qvell got the solution?

    • @Trick-theDev
      @Trick-theDev 15 днів тому

      i removed player and dragged n dropped my player in as a string instead but i still cant get the turret to fire lol

  • @Kumo0
    @Kumo0 2 місяці тому

    Why isn't working on 3d

  • @kentmiggalen9756
    @kentmiggalen9756 6 місяців тому

    helped me

  • @Frazer213
    @Frazer213 10 місяців тому

    In my game im doing a ray cast from the front of an enemy, if the player hits that ray cast the enemy will shoot. The issue I'm having is anything and everything the Raycast collides with comes back as true.

    • @Frazer213
      @Frazer213 9 місяців тому

      i ended up figuring it out, it was a combination of bad checks that were not being identified correctly@ddwaveking

  • @moonnight9474
    @moonnight9474 Рік тому +1

    good job

  • @MrBillathong
    @MrBillathong 8 місяців тому

    This code for the bullet desnt work. When I implement it, the bullets are coming out at a 90 degree angle to the right of the ray cast. I followed the code as 1:1 as possible, yet I have no idea what is causing this ( on v4.2.1)

    • @16bitdev
      @16bitdev  8 місяців тому

      update direction of the bullets to player while spawning them, by default its Vector2.RIGHT

  • @Akamo.
    @Akamo. 9 місяців тому

    Cheers man

  • @puppetindustries1200
    @puppetindustries1200 Рік тому

    Why is the raycast moving using player movements instead of targeting player?

  • @3asak1
    @3asak1 Рік тому

    Invalid set index 'target_position' (on base: 'RayCast2D') with value of type 'Vector2'.

    • @16bitdev
      @16bitdev  Рік тому +1

      if you are using godot 3.5, then its "cast_to", in godot 4 they renamed it to "target_position", change the property name from "target_position" to "cast_to"

    • @3asak1
      @3asak1 Рік тому

      @@16bitdev thanks :D

  • @marianmotorina
    @marianmotorina Рік тому

    Thanx

  • @DucNguyen-k4g
    @DucNguyen-k4g Рік тому

    How set raycast collision mask 1 and 2 on code

    • @16bitdev
      @16bitdev  Рік тому +1

      RayCast2D(node).set_collision_mask_value( Layer_number, true)

  • @saveriov.p.7725
    @saveriov.p.7725 Рік тому

    How do you get the raycast to show up as a red arrow?

    • @16bitdev
      @16bitdev  Рік тому

      if raycast detects collision it automatically turns red, make sure the collision body and the raycast are masking properly.

    • @EricAbroad
      @EricAbroad 7 місяців тому +1

      turn on Debug -> Visible Collision Shapes on the top bar in Godot 👍

  • @elkekeeranders2840
    @elkekeeranders2840 11 місяців тому

    how do you make the bullet kill the player?

    • @16bitdev
      @16bitdev  11 місяців тому +1

      for player, give a variable 'health', and a function that decreases health (func take_damage()) , now the bullets are Area2D, they have a signal called body_entered(body), so when a player comes in contact with the bullet, body.take_damage() gets called, when health goes

    • @elkekeeranders2840
      @elkekeeranders2840 11 місяців тому

      thank you!@@16bitdev

  • @LNA881
    @LNA881 Рік тому

    Not sure why but my bullets aren't going in the scene

    • @16bitdev
      @16bitdev  Рік тому

      first instantiate the bullet scene,
      then set its position,
      then add it to the scene by typing,
      get_tree().current_scene.add_child(bullet)

  • @ShitPostUltimate
    @ShitPostUltimate Рік тому

    Thank Man my script od working 💲