Raycast from its current position to a global position

Godot Version

Godot v4.3.stable - Debian GNU/Linux 12 (bookworm) 12 - Wayland - Vulkan (Forward+) - (software emulation on CPU) llvmpipe (LLVM 15.0.6, 256 bits) - AMD Ryzen 3 7320C with Radeon Graphics (8 Threads)

Question

I have a CharacterBody2D with a RayCast as a child, and I want to have a function that sets the raycast’s target position to another global position and returns where it collides along the way. I’ve tried doing that with

func target_collision_point(target: Vector2) -> Vector2:
	self.target_position = self.global_position - target
	self.force_raycast_update()
	return self.get_collision_point()

but it doesn’t work and set it to a very far away position, not even in the correct direction.

try this:

target_position = to_local(target.global_position)

thanks that worked! how would I extend a ray cast in a specified direction (normalized vector) until it hits something?

You just set the distance as high as you want it to reach. It will automatically return the first collider it hits

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.