Change the width of an object depending on position of collision?

hello I would like to know a simple way to change the width of object laser and its collision when in collision with another object like in the animation?

animation
animation

Do you know the zombie laser example? GDevelop 5

1 Like

thanks I had a go at raycast given the example but failed to get it to work as it should or how I wanted :sweat: the other problem if I manage to get raycast working how do I use to change the laser object width?

Okay, the raycast itself looks intimidating but it works actually quite straightforward. Here in this example I use a raycast horizontally ( angle 0) against an obstacle and if it hits the obstacle a marker is drawn at that point.

Probably you know that but just to make sure that you get it working:

  • start_laser.X and .Y is the position of your point on the sprite from which you want to shoot the laser;
  • angle in your case will be 0;
  • distance is clear, in your case it is probably below 800;
  • if you have several objects which the raycast should check you can put them in a group
  • the X and Y coordinates of the impact are stored in two variable which you would use for drawing your laser (in the example I just place a marker there)

Did you manage to get there?

1 Like

For drawing the laser it seems that the shape painter object is the easiest way to get the desired effect. You could draw your laser line with 6 (I guess) objects stacked on each other. The muzzle flash and the impact would be two additional sprites.

I quickly tried to draw the laser with a sprite, by changing its width through comparing the position of the starting point with the contact position. The result is not consistent but maybe it is a possibility to get a similar effect with less objects.

thanks I managed to get there! I understand about using raycast will try 6 draw objects different heights to match the laser sprite a bit of work but hopefully should work out :sweat_smile:

1 Like

I have a problem with draw shape object and raycast for some reason keeps doing this
example
Animation

how do I fix so the draw object follows laser1 start position and end y position where the laser impact?
events

1 Like

For drawing the laser from your point you need to uncheck this box in the shape painter setup:

The other problem is - and I just realized that now - that if there is no new collision, the shape is being drawn between the laser starting point and the last contact position stored in the coordinate variables. The zombie laser example is using state variables (0 and 1) to check if there is a contact or not. If not the line is just drawn horizontally across the screen. So you would also need to use a variable (preferably boolean) for that.

1 Like

thanks for your help I unchecked draw shapes and added a boolean and now almost working :sweat_smile: except draw object doesn’t move with lasers x position?

events edit

There shouldn’t be any problem with the laser being drawn when the object moves along the x-axis (I checked it in my example and it works). So I guess you have to check if there is no typo in your position names etc. If I look at your events I see at least two lines which look “suspicious”: com.X and comp.Y; laser.X() and laser1.PointY(“Point”).

1 Like

you’re right I had a typo draw object laser. fixed thanks working great now!

1 Like

there is one thing that I noticed the collision of the raycast is like 1px in middle casting when laser is directly in middle. is there a way to increase the height of the raycast so collision happens right when laser hits the objects?

collision not right
Animation
events

That’s not possible. We have to imagine the raycast like a thin line (I guess in reality there is not even a line, just two points). You could use more than one raycast (e.g. top, middle, bottom) but then there is still the question how to draw the laser.

At the moment I can just think of taking the collision position of the upper or lower raycast and subtract or add a few pixels along the y-axis to draw the laser in the middle. Not sure if that works well and there might be a better solution. :thinking:

Maybe someone else can chime in with a suggestion.

I tried your first solution by placing a copy of the raycast and draw object lasermask at a different y point from laser1 but it wont trigger both recast collisions at same time and can only use one of the draw lasermask objects as a mask on laser7.

Yes, you probably need boolean variables to determine which of the raycast-position variables should be taken into account for drawing the laser.

Maybe something like

  • upper_raycast is in contact with obstacle,
  • set upper_raycast variable to true
  • if true, draw the laser to position of upper raycast contact variable X and Y + 24
  • set upper_raycast to false

But as I wrote, this is just a rough idea which seems to be quite tedious to implement and maybe there is a better solution for that.

ok I have the upper raycast but not sure how to have it’s raycast also change the middle raycast on collision?

example upper should also change middle raycast x position
Animation

events

That’s what I suggested in the previous post. You have to make it so that on contact with the upper and/or lower raycast the laser is still drawn from the middle point. For doing that you need to subtract or add pixels from the y-position variable if your upper or lower raycast is in contact, in your case in the draw event: Variable(contactyy) + 24(?).
After thinking again about it, using booleans for that, as I suggested above, is not necessary.

ok I got draw lasermask2 to the middle and have realised that I have it so the draw lasermask is the mask for laser7 and when I try to add another raycast to mask laser7 it won’t mask I can only have one or the other working?

I think there shouldn’t be any limit. You should be able two draw as many lasers as you want.

I can have more than 1 draw object the problem is only 1 will mask the beam and the lasermask is what I have to mask the beam