2D Top-Down: Dynamically stretch wall to enclose rectangular area?

I’m working on a top-down mobile game in which a player can swipe either vertically or horizontally to spawn a wall that then grows automatically until it reaches the outer walls of the level or another, previously spawned wall. The level is a fixed, rectangular area (no scrolling or anything) enclosed by 4 walls.

As an example, here’s what it should look like if a user swipes vertically in one location the horizontally in another before the first wall is built:

alt text

Additionally, certain objects within the game can destroy a wall if they collide with it while it is still expanding. Once the wall has fully expanded, however, it is invincible.

I’m new to unity and game development in general, and I’m having some difficulty implementing this system (which seems like it ought to be simple).

Currently, my solution is as follows:

  • When a user swipes, I determine the
    touch position and swipe direction.
  • Spawn a ‘barrier’ sprite at the initial touch location.
  • Stretch the sprite in the desired direction until both ends have
    reached another barrier (or outer
    wall), shifting the center of the
    sprite as needed to compensate for
    one end reaching a barrier before the
    other (as in the vertical barrier in
    the gif).
  • When both ends reach another barrier, the wall stops expanding.

However, the main problem with this method is that it relies on colliders and shifting positions/scales within ‘Update,’ so the walls will continue extending/moving for a few frames after the collision actually occurs. Of course, if the rate of expansion is too high, collisions won’t be detected at all.

alt text

I’ve done quite a bit of googling and think that I may be able to use raycasting to help tidy things up, but I’m not entirely sure how to go about that, and I suspect I may be missing something that would make this much easier overall (maybe using a grid and adding multiple walls instead of stretching one?).

In summary, my question is whether this is an effective way to achieve what I want to do, and if so, could someone help get me started with how I could more precisely control the expansion with raycasts or another tool?

Yous seem to have it pretty figured out, so I’m not sure what you need help for. I will just say though, that raycast have the significant advantage to return (among other things) the distance origin → raycasthit, so you can predict exactly how much you need to increase your barrier.

That would mean something like

//2 times if still expanding from both ends
//raycast doesn't have to start from barrierEnd, you just need to be able to compute the distance between the end of the barrier and the wall
RaycastHit2D hit = Physics2D.Raycast(barrierEndPosition, direction);
if (hit.distance < minimum) {
  barrierIncrease = distance;
else {
  barrierIncrease = barrierIncreaseSpeed;

Not sure that helps you much, but like I said, you have it pretty much figured out.