How to make an object know if it is being hit by a raycast

Godot Version

4

Question

I want to use raycast for an attack so if the raycast hits an enemy that enemy gets hit, but I only know how to make the raycast get the enemy it hit and to know if it hit something. How do I make the enemy know when it's been hit by the raycast

You can call a function on the enemy on the script that the raycast uses to detect enemies. The function would tell the enemy it has been hit.

1 Like

So when the rat cast detects an enemy, get that enemy and than use the get enemy thing the rat cast does to call that enemies func

You don’t seem to be the original poster of the question, but yes, you can use the get_collider() function to get what the RayCast hit, which would be a body that represents the enemy, which you can then call a damage() on.

1 Like

sorry I was on an alt account and forgot I was on an alt. Golden form is me

func _physics_process(delta):
if raycast.is_colliding(true):
raycast.get_collider(target)
target.beenraycasted()

I made this but its doesn’t work but I think it can give you an understanding of where my heads at and what I’m doing wrong

If you use a code like this:

func _physics_process(delta):
    if raycast.is_colliding(true):
        raycast.get_collider(target)
        target.beenraycasted()

Have you made sure the target you’ve hit has a beenraycasted() function defined? You cannot call a function if it’s not defined in the object you’re calling it in.

When you say it doesn’t work, are you talking about errors in the editor console? If that’s the case, it would be convenient to share them.

it is defined, but its crashing because of (true) and (target) I kinda knew this wasn’t going to work when I coded it but this is the closest I could code to what I wanted to code

I don’t even know if by putting target into the brackets like that will make the target the same as what the raycast is getting

Well… it seems to me you’re struggling a lot with the gdscript syntax, have you watched some tutorials or tried doing more simple stuff than a raycast?
I guess your code can be easily fixed, but I don’t see myself writing a solution if you don’t even understand it as you will then struggle to use and iterate on it afterwards :sweat_smile:

FOOL… I am the master of things! I have an impressively rough idea of gdscript works and a far weaker knowledge of syntax even is! there are only super basic tutorials on how to download godot, no tutorials that teach have been created yet! I must learn though examples, once someone shows me how to do something I will ask questions! and put it into practise to learn how it works! …also when asking if it was defined you just choose to say the most common problem that I already new of leaving me with no new info! …so hard to learn in a place filled with so many people teaching people who no near to nothing and barely anyone teaching the people who know a little bit, because it is easier to teach simple things sometimes! I’d love if I could find tutorials that could help me… sorry about this big message for no reason, I just watched one piece and worked out :sunglasses:

If you’re still looking for quality tutorials I can recommend Godotneers
https://m.youtube.com/@godotneers
Has a gdscript basics tutorial in two parts that is really solid. They are also not too dry, promise. It is very worth it, afterwards you will understand sixrobins solution and why it should work.
Best of luck, developing is great fun once you get past the absolute basics.

1 Like

Since nobody has provided a concrete fix yet:

The problem is that you are passing (the nonexistent?) target to get_collider. get_collider does not take a parameter. What you want is the value that is ‘returned’ by the function. The same goes for is_collding:

func _physics_process(delta):
     # you can/should remove the `== true`
     # I just left it here so it's clearer what's happening
    if raycast.is_colliding() == true:
        var target = raycast.get_collider()
       # optionally check here if the target is actually an enemy
        target.beenraycasted()

As the others have mentioned, you should probably look at some tutorials/references. I think the ‘Getting Started’ section in the documentation is a great place to start:

1 Like

I checked that guy out and I’ll watch his videos, if you guys know any more channels like this, please link them. Oh, and I did know what syntax was just didn’t know it was called syntax

I ended up using area hitboxes already :grimacing: But thanks