Skip to content

Caravan Manager

Started looking at using splines in Unity. I found this tutorial by AnanDEV which seemed to be exactly what I needed. I used his custom script but stripped down, since I don't care about the rotation (since the sled will lerp towards the target independently).

You can see the sled following the target that moves along the path.

Sled following the target

Because the target and the sled are independent, this should ease any wonkiness if the target has to jump (for instance, if the next path isn't perfectly aligned). The sled will (with it's own smoothing) will catch up to the target. There might be slight speed changes but unless the target jumps an incredible distance, I don't think the players will notice.

In the gif below, the sled starts quite a ways away from the target and you can see it catch up, then slow down as it reaches the target. Hopefully, we don't have nearly that big of a gap in the final game!

Sled catching up to the target

One thing to note: I did have to move the GameManager in the Script Execution Order for the project to ensure that it was initialized before any of the other things. This will help ensure that the base manager for the game is ready and available to all of the other scripts.

The sled is working pretty well on straight paths but what happens if we put some curve into the path? Well, frankly, it looks goofy.

Sled following a curve without adjusting rotation

But, after adding some logic to point in the right direction it looks pretty decent!

Sled following a curve with adjusting rotation

The next thing to get working is getting the sled target to jump to the next tile's path when it reaches the end of the current tile's path. This was a little trickier than I had originally thought. It sounded simple when I first thought of it, something like:

  1. Starting at index 0, grab the Path off the first tile out in the list
  2. Move the target along the path
  3. When the target gets to the end of the path, increment the index and grab the new path
  4. Rinse and repeat

However, I immediately ran into a problem with using the index. Because the list of loaded tiles is constantly evolving (new tiles are loaded, old tiles are unloaded) I can't guarantee that the current index still represents the same tile in the list when I go to check it again. This means that I can't guarantee that if I increment the index that it will point to the "next" tile. I pondered a few ways to do this but ultimately settled on using a Linked List. This way, I could get easy references to the next tile in the sequence. It also makes it easier to remove the tiles at the beginning.

This was pretty easy to refactor, I just had to account for the fact that the list holds a list of nodes that have the actual tile values in them, rather than the tile itself. In the code example below, you can see the use of the .Value property on the node pulled from the list

public void LoadNextTile(Tile tilePrefab)
{
    var newTileOrigin = Vector3.zero;
    if (LoadedTiles.Count > 0)
    {
        newTileOrigin = LoadedTiles.Last.Value.transform.position + _tileSpawnOffset;
    }

    var tile = Instantiate(tilePrefab, newTileOrigin, Quaternion.identity);
    LoadedTiles.AddLast(tile);

    while(LoadedTiles.Count > _maxLoadedTiles)
    {
        var oldTile = LoadedTiles.First;
        LoadedTiles.RemoveFirst();

        Destroy(oldTile.Value);
    }
}

The code ended up working perfectly after the refactor and the sled transitioned to the next time really well.

Sled transitioning to the path on the next tile

Once I got everything tidied up, I wanted to add support for having multiple sleds in the caravan. This proved to be an interesting problem. How do you get the sleds to follow each other and not pile up into the same spot on the path? I had to do some creative checks to ensure that each sled did not start moving along the track until the one ahead had gotten at least some distance along the path.

This was achieved by adding a reference to the sled to the one ahead of it then checking to see how far it had gone on the path. Once it has moved far enough along the current sled's target will move to the path and start following it.

All the sleds falling into a single file line