make a sprite stop at the body (Rescale) when Raycast2D detects a body

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

Hi There :slight_smile:

i have a laser attached to a gun in a 2D game and the question is
how can I make the Laser (Sprite) Stops at the detected body?

i don’t want to see the sprite after the detected body

i’m Using Raycast2D to Detect Bodies And The Laser is a Sprite

i used a lot of methods but none of them worked like i wanted

func laser( ):
if $Cube/Laser_System/Laser_Ray.is_colliding():
$Cube/Laser_System/Laser_Ray/Sprite.set_scale(Vector2(12.391(-1),0.304))
else:
$Cube/Laser_System/Laser_Ray/Sprite.set_scale(Vector2(12.391,0.304))

What math Method Should I Use instead of decreasing scale.x by constant number?

picture of the problem
link of the pic in case the browser didn’t show it

:bust_in_silhouette: Reply From: Mrpaolosarino
onready var Raycast = $Cube/LaserSystem/LaserRay
onready var Sprite = $Cube/LaserSystem/LaserRay/Sprite
func laser():
  if Raycast.is_colliding():
    if playerfacing == "right":
       if Raycast.get_collision_point().x != Sprite.global_position.x:
          Sprite.scale.x += 1
        elif Raycast.get_collision_point().x == Sprite.global_position.x:
          pass
    elif playerfacing == "left"
         if Raycast.get_collision_point().x != Sprite.global_position.x:
          Sprite.scale.x -= 1
        elif Raycast.get_collision_point().x == Sprite.global_position.x:
          pass

Tell me if this work

Hello and thank you for your Respond :slight_smile:
Unfortunately the code didn’t worked correctly
the Laser Sprite didn’t stopped at the body which raycast detected
instead of that the sprite scale.x increased to the end of the viewport (Screen)

example ( a draw illuminate the idea that’s I’m working at )

in Godot Editor I put The Sprite exactly on the start of the raycast

Zylo_X | 2021-05-16 20:32

I think the reason is that the sprite is not relative. Make sure the drawing extend until the edge of the sprite box, if not then the illustration will happen

Mrpaolosarino | 2021-05-17 07:10