Can I spawn a 2D segment form a point to a mouseclick and have the sprite adjust to the shape?

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

Hello, first time asking something.

I am trying to spawn some kind of hand, like a grapple, between my character and where I clicked on the screen. On event, I create an instance of a Rigidbody with a CollisionShape2D that has a segment shape. I put the A variable of the segment on the character and the B variable on the mouse position.

Thing is, I don’t understand how I can make my sprite fit this shape that is not know beforehand. It is hard to tell if my script is working when I can’t see the segment.

Yes, I am very new to the engine. Maybe it’s a simple problem and I’ll be very grateful for an answer!

Do you have a graphical mock up? I don’t understand what do you mean by segment shape created when clicked. Maybe a similar game video?

decepticlown | 2021-09-23 13:50

I tried putting an image that I think can help. I’ll try to better illustrate what I’m trying to do in the first place. I’m trying to make a mechanic which is that when a player clicks on the screen, a line is created between the character and the position of the click.

I found that the SegmentShape2D shape for the CollisionShape2D node has two variable which correspond to the position of it’s two ends, so my idea was to, on click, create an instance of an object with the segmentshape and modify it’s variables so that they fit the character’s position and the click’s position. It might be a very inneficient way to do it, idk.

I could try sketching my idea, it’s pretty clear in my head but I don’t know how I can import the image in the forum.

Thanks for helping me

Other video game with the same concept kinda

Mallau | 2021-09-23 18:00

Ahh, so its like a grapple! Well for position of two points you should use raycast as mentioned below in answer. Grapple is hard to implement if created shape is softbody. You can try softbody node. But I don’t know how you can make it behave with two constraint or whether it will hold. As long as just drawing a line goes raycast will give you all the information you need.

decepticlown | 2021-09-26 11:42

:bust_in_silhouette: Reply From: skysphr

Check out RayCast2D. You will probably need to use its get_collision_point() to build your grapple. There’s also a basic Ray-casting guide in the docs.

Sounds good, thanks!

Mallau | 2021-09-27 17:01