0 votes

Hi There :)

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/LaserSystem/LaserRay.iscolliding():
$Cube/Laser
System/LaserRay/Sprite.setscale(Vector2(12.391(-1),0.304))
else:
$Cube/LaserSystem/LaserRay/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

Godot version 3.2.3
in Engine by (118 points)
edited by

1 Answer

+1 vote
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

by (385 points)

Hello and thank you for your Respond :)
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

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

Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.