Connecting textures

Hi there,

I’m currently making a conveyor belt system for my game. I’ve managed to get the belts working fine but there’s an issue with placing them.

Graphically, they are incorrect.

As you can see.

Sometimes they will connect perfectly, but most of the time it’s just wrong.

The code I have written is not good. In fact, among the worst I’ve ever written. It’s just spaghetti, but I just wanted to get something working and work on it to improve it. Saying that, I can’t even figure out how to get this version working.

I’m not asking for help in my current code, but rather a different approach to this.

Currently, I have a collider on each side of the conveyor, when one detects another belt it will check that side and update to connect to the previous conveyor. There’s a whole host of issues with it, considering that the class is 750 lines long!

If anyone has a different approach to this, please feel free to comment.

Thanks.

1 Like

Well for sure, having colliders is going to be one of the worst ways to go about finding neighbors if you’re on a grid. What you should do instead: use the grid! When you create a conveyer belt, put it into a 2-dimensional array at its x,y position (use RoundToInt for this so that you don’t get screwed up by floating point imprecision).

Something like the following. (Depending on your scene setup you’ll probably want to modify the x and y so that they actually fit into your grid - negative numbers are no good, etc)

public class ConveyorBeltManager : MonoBehaviour {
public static ConveyorBelt[,] grid = new ConveyorBelt[50,50];
}

public class ConveyorBelt : MonoBehaviour {
void Awake() {
int x = Mathf.RoundToInt(transform.position.x);
int y = Mathf.RoundToInt(transform.position.y);
ConveyorBeltManager.grid[x,y] = this;
}
}

When the objects are all placed and in the array, loop through all of them, having it check the x,y position of each of its neighbors. (If the objects are expected to be placed/removed during the game, feel free to run this algorithm every frame - checking arrays like this is super fast.)

bool north = (ConveyorBeltManager.grid[x, y+1] != null); //whether we have a north neighbor
//repeat for the other directions

When you do this, you’ll have 4 bools, one for each direction, for whether or not there is a belt in that position. So now you need to turn those 4 bools into a reference to a sprite. There’s plenty of ways you can do this with varying degrees of complexity and efficiency. One of the conceptually simplest (albeit not the most elegant or most efficient) ways would probably be to use the filenames of the sprites as a dictionary key; if you name these files using a precise naming scheme, you can turn your 4 directions into a string.

//on the manager
public Sprite[] allSprites; //drag all your sprites into this array
public Dictionary<string, Sprite> spriteLookup = new Dictionary<string, Sprite>();
void Awake() {
foreach (Sprite s in allSprites) {
spriteLookup.Add(s.name, s);
}
}

// when determining neighbors
bool north, south, east, west; //fill as shown above
string filename = $"North_{north}_South_{south}_East_{east}_West_{west}";
Sprite mySprite = ConveyorBeltManager.spriteLookup[filename];

If your filenames are in the form of “North_True_South_False_East_True_West_False”, this should fetch the correct Sprite for your situation. (Make sure that you’re careful of capitalization, order, etc are precise, and that you have all 16 possible combinations) You’ll probably want to add some debug outputs of these string, some error tolerance (like checking spriteLookup.Contains before accessing it), etc.

2 Likes

Perfect. Just needed another angle on this. Thanks alot :slight_smile:

I was able to get some pretty nifty looking conveyors with the technique above.
Obviously there was some tinkering to be done in that I didn’t want them to ALWAYS connect if there was a conveyor that wasn’t even pointing toward another. I can say that I’m very happy with the result.

I spent hours researching this topic before asking here, there’s basically NOTHING on how to do this. So hopefully the comment above will help anyone in the future.

Here’s a screenshot.


(The misconnecting textures, I.E. one pixel off is fixed now :))

1 Like