#Raycast Help

12 messages · Page 1 of 1 (latest)

agile plume
#

How do I get a Raycast from an object to trigger code on a separate object? I've got a thing working with collision shapes, but for the life of me I cannot get the same to work with Raycasts. The screenshot is the code I'm trying to get the Raycast to trigger.

queen eagle
#

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);
agile plume
#

What is '{the raycast result};'? the damage number I'm trying to impose?

queen eagle
#

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
queen eagle
#

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);
agile plume
#

doesn't seem to do anything, here's my code for additional context.

queen eagle
#

You would need to call the do_damage function

#

probably somewhere in fire

agile plume
#

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

queen eagle
#

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

agile plume
#

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.