Raycast not detecting collision with physicsbody

Godot Version

4.4.1

Question

basically the title
sometimes the raycast dosent recognize that there is a physicsbody collision when updating. I’ve tried inserting await get_tree().process_frame some places in the code but that didnt seem to have an effect

for clarification it seems to be the spell_raycast that has the issue and not the spell_raycast_bounce

(please ignore the blue path thats pointing down and to the right I forgot to disable paths)

heres whats happening :

White collision is StaticBody
Blue collision is CharacterBody

Code:

func _physics_process(delta : float) -> void:
    ray_bounce()


func ray_bounce() -> void:
# ray cast
	global_position = parent.global_position + parent.spell_direction.normalized() * 30

	if spell_raycast.is_colliding():
		if spell_raycast.get_collider() is StaticBody2D:
			var collision_normal = spell_raycast.get_collision_normal()
			var collision_pos = spell_raycast.get_collision_point()
			var reflected_dir = spell_raycast.target_position.bounce(collision_normal).normalized()
			var ray_length = spell["size"] - (spell["size"] - to_local(collision_pos).length())

			print("IS COLLIDING")
			spell_raycast.target_position = parent.spell_direction.normalized() * ray_length
			spell_raycast_bounce.global_position = collision_pos
			spell_raycast_bounce.target_position = reflected_dir * spell["size"]
			


		elif spell_raycast.get_collider().is_in_group(hostile):
			print("is colliding with enemy")
			var collision_pos = spell_raycast.get_collision_point()
			var ray_length = spell["size"] - (spell["size"] - to_local(collision_pos).length())
			spell_raycast.target_position = parent.spell_direction.normalized() * ray_length

	else:
		print("not colliding")
		spell_raycast.target_position = parent.spell_direction.normalized() * spell["size"]
		spell_raycast_bounce.position = spell_raycast.target_position
		spell_raycast_bounce.target_position = Vector2.ZERO

I cannot for the life of me figure out why this is happening

	var ray_length = spell["size"] - (spell["size"] - to_local(collision_pos).length())
	# is the same as:
	var ray_length = to_local(collision_pos).length()

After that, you’re setting the length of target_position to ray_length. So, you’re basically setting the length of target position to the collision point. Thus the raycast won’t detect the enemy if the new collision point would be (even slightly) further away then the current collision point is.

If the spell_raycast is used to detect enemies, is there really a need for you to change its length?

Fixed it with a move toward to account for the small difference if isnt exact.

If the spell_raycast is used to detect enemies, is there really a need for you to change its length?

i have some other spell attributes like pierce so unless it can pierce i would like it not to

Thank you for the help! :slight_smile:

1 Like

But get_collider()/get_collision_point() only return the first object hit / collision point anyway. So, unless you’re adding previously hit targets as exeptions or something, it won’t make a difference if the ray pierces through the enemy or not.