Matching a sprite width to a ray

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By demon3o5z

I’m trying to get a character to fire a laser that has a set maximum range, but stops and adjusts length when it hits a surface. I know there are tutorials for making lasers using rays, but I’ve also got a sprite that needs to adjust it’s length so that it matches the range of the ray.

here’s the logic of what I’m trying to get to:

I have a sprite that’s the child of a ray

if ray is not colliding
set sprite.texture width to ray length
if ray is colliding
set sprite.texture width to the difference between ray’s origin point to collision point

I haven’t even gotten to adjusting the length based on a collision point because I can’t get the texture to match the ray.

Any suggestions would be appreciated.

:bust_in_silhouette: Reply From: Magso

This would be easier with the Line2D node; adding the player’s position and the raycast hit position.

line2d.add_point(player.get_global_position())
line2d.add_point(raycast.get_collision_point())

For a sprite to do this it would need to be scaled, moved into the centre and rotated.

sprite.scale.x = player.get_global_position().distance_to(raycast.get_collision_point())
sprite.set_global_position(lerp(player.get_global_position(), raycast.get_collision_point(), 0.5)
sprite.rotation_degrees = rad2deg(get_angle_to(player.get_global_position()))

I took a second look at Line2D after posting this and totally see what you’re saying. And you’re example really helps. Thanks.

demon3o5z | 2020-03-02 00:49