GameDev Blog: Goblin Rules Football #27: Slope Direction Tiles and Making Them Do Stuff

In my previous post I mentioned wanting to include slope information on my tiles, possibly doing it through scriptable objects. Scriptable objects seem like that would be a powerful tool to use in Unity, but unfortunately I didn’t actually do any of the tutorials and went a different route for “storing” “data” on the tiles, so I still have no idea what scriptable objects really are or how to use them.

Anyway, the way I went about “storing” data in tiles is by creating a “DirectionTileManager” that loops through all the directional tiles, which are in their own tilemap, and then cross referencing a “map” of what slope direction and slope speed is associated with that type of tile. Then, when a ball either rolls over a tile, or bounces on a tile, the ball calls the DirectionTileManager instance (it’s a singleton, which I’ve been told by programming tutorials is “bad,” but whatever it seems to work well whenever I use them) and asks if there is any slope data for the tile the ball is on. The DirectionTileManager returns the data to the ball, and the ball adjusts its rolling trajectory or bounce direction/distance accordingly.

The directional tiles look like this:

The color and size of the arrow indicates whether the slope is a “slow,” “medium,” or “fast” slope. The dashed lines are to show the boundaries of the slope, so the player can easily tell where the ball will be affected by a slope and where it will not be affected by a slope. In game, it would look something like this:

Once I had the tiles displayed to the user, I then need to have the slops actually affect the ball. I had created something earlier to do that, but it turned out to not actually work correctly. The old way would change the direction of the ball’s roll, but if the slope was either the same as the balls movement vector or in the exact opposite direction, it didn’t affect the ball’s roll/movement at all. As in, if the ball was rolling up a hill, it wouldn’t slow down, and if it was rolling down a hill, it wouldn’t speed up. Bad!

So, I had to go about fixing that. The reason nothing was happening is all that I was doing before was adding to the movement vector and then normalizing it. So, adding (0,1) to (1,0) would result in (1,1), and the ball would move in a new vector of (~0.7,~0.7) after being normalized. However, if you add (1,0) to (1,0) you get (2,0), but that normalizes to (1,0) and nothing changes! The fix, then, was to use the slopes to add “force” to the ball that would affect the ball’s rolling speed.

Unity’s physics engine does all this, but I’ve been “rolling my own” physics-type stuff for this game mode so far and decided to again do that. I did spend a few hours trying to use the Unity physics stuff but I didn’t get it working the way I wanted so I dropped it. Anyway, what I did was some Vector Math or something (I didn’t do much math in school and don’t remember Vectors…), creating a “force” vector for the slop with a magnitude based on the slope “speed,” and then added that to the ball’s movement vector with a magnitude representing its movement speed. So, if a ball was rolling up a hill, the slope vector would essentially “subtract” from the movement vector, slowing the ball down, until it caused the ball to eventually roll backwards down the hill! Wow!

Besides slopes affecting a ball’s roll, I also wanted it to affect a ball’s bounce off a ground. A ball should, I believe, bounce differently off a flat surface compared to a sloped surface. Similar to the rolling stuff, created a vector for the slope to affect the direction of the bounce, added them, and bam! The ball bounces in different directions based on the ground slope.

So, here’s a video of me testing some of that:

And a video of me trying to make a putt through some slopes and making it! After only 2 tries!

Wow! Amazing stuff! Truly, I am impressed. It also only looks a lot like Sports Story’s slopes, and not 100% exactly like it (they have their slope arrows animated, while mine aren’t…). I do need to update the color for the “slow” direction tiles. The light blue is kind of hard to see on the grass. Something with a little more contrast would be good…

Next Steps…

Maybe start trying to make it so you can play a real “round” of golf on a single hole? Track number of strokes, have a “par” for the hole, tell the player if they got a birdie/eagle or if they stink and got a bogey? Idk…

Smell ya later, nerds!