Need help making raycast2d gun

Question

I am new to game dev and I’m trying to make a hitscan shotgun for my top down 2d game but all the tutorials I’ve found are either really old or aren’t very applicable. I understand that I need to use raycast2d nodes but I don’t know how to set them up so that they scan the direction and range i want them to.

It is really easy to create this, if you have basic programming knowledge, then you can learn about raycast by reading docs

1 Like

i did read the doc and i didnt understand how to apply it to what im trying to do

It’s as easy as setting up your collision layers and running is_colliding() and checking if the return is true or false. If it still doesn’t work and you know when the check is supposed to happen you can force_raycast_update()

1 Like

how do i write this in the script to make it work?

Read the documentation.

i already said i did and i didnt understand how to apply it

Hope you had setup the raycast so add this codes in your player script:

if Input.is_action_just_pressed("shoot"):
   #Play sounds & particles...

   if $Raycast.is_colliding():
       if $Raycast.get_collider().has_method("take_damage"):
           $Raycast.get_collider().take_damage(20)

First read the codes to understand, it is important.
Do not forgot to define “take_damage(value)” function in enemy script, and to define “shoot” input map.

how do i define take_damage ? like what should i be writing in the script?