#Raycast Help
12 messages · Page 1 of 1 (latest)
In this instance I assume your raycaster is specifically for calling this function. In which case you could
var raycast = {the raycast result};
# check if it hit anything
var hit = raycast.get('collider', null);
# if it did and it's the object you're looking for
if hit and hit.has_method('health'):
hit.call('health', damage_amount);
What is '{the raycast result};'? the damage number I'm trying to impose?
Well for me i use
func _ray_cast(from, to):
return get_world_3d().direct_space_state.intersect_ray(PhysicsRayQueryParameters3D.create(from, to));
``` to raycast and that returns a result per the docs and that result would be `{the raycast result}`
https://docs.godotengine.org/en/latest/classes/class_physicsdirectspacestate3d.html#class-physicsdirectspacestate3d-method-intersect-ray
Inherits: Object Inherited By: PhysicsDirectSpaceState3DExtension Direct access object to a space in the PhysicsServer3D. Description: Direct access object to a space in the PhysicsServer3D. It's u...
the damage number I'm trying to impose
would bedamage_amountin this example
if you're using a RayCast2D/3D you'd do something like this
@onready var raycast = $RayCast2D; # or whatever your node is called
func do_damage(damage: int):
# check if the collider hit anything
# then check if the collider is the object you're looking for
if raycast.is_colliding() and raycast.get_collider().has_method('health'):
# Call the method you're looking for
raycast.get_collider().call('health', damage);
doesn't seem to do anything, here's my code for additional context.
if I put it in empty 'do_damage()' I get an error and it won't let me launch the game. I put 'damage' in the brackets and it'll launch, but no effect
do_damage takes the argument damage which is the amount of damage to do to the target
the line Cannon.get_collider().call('heath', damage); calls the function named health on the target and when you showed me that method it takes 1 argument damage
I have a couple dummies set up for testing, when their health hits 0 they're removed from the game, the Cannon does it's animation (where it's enabled for a bit then disabled) but nothing happens to the dummies.