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