Let’s Get Physical

The day after I wrote my previous post I opened up the Road Bastard project, and realised right away that I was sick of the way it looks. Almost every project I start gets made with Unity’s standard primitives – cubes, cylinders and spheres etc. and I tend to just throw a few plainly-coloured materials at them to make sense of things. It’s a really quick way to get a project up and running, but it does mean they tend to all look the same. And I’m a bugger for picking the same colours. I like most of my colours to be a bit on the blue side, so I generally have bluish grey for metal or concrete, and bluish greens for grass and trees etc. and I’m now thoroughly bored of it. So the first thing I did was find a couple of better materials for the road and the grass that I’d downloaded free from the Asset Store, and then darkened up the remaining flat colours a bit to make it look a little different. I’ve no idea what the final art style will be, but this at least isn’t boring me silly:

(There is an issue with these materials in that something about the way they’ve been made means that lights and effects don’t show up anywhere near as well as they should, so I will need to replace them again, shortly.)

Next up I added some camera shake for when the car is on the grass. I have this in almost all my projects – it’s a very quick way of adding a bit of visual interest – so it was just a case of copying and pasting some code from another project. I forced this in, by just applying it when the car is a certain distance from the centre of the road for now. I stupidly didn’t think to also check that the car is moving, so it shakes even if you sit still on the verge. Never mind – needs doing properly at some point anyhow – the point was more to get the code up and running.

I also got around to buying a fog asset from the Asset Store. I added this with default settings, and it didn’t seem to show up, but then I remembered the new materials don’t like special effects, so I tried turning the lights out:

Looks like it’ll work, but I’ll play with this later. I’ve bigger fish to fry.

Next up I decided to do something a bit more important – change the control of the car over to being physics-based. This actually involved re-working pretty much everything. If you’ve read the first post on this game you’ll know that the game was originally meant to be a very simple lane-changing game. Because of this, it was written in a very cheat-y fashion. Basically the player’s car never moved down the road – the road moved towards the player. As the player accelerates the road just moves towards them faster. Likewise all the other cars are moving at their own speed, but also towards the player at whatever speed the player is currently at. I can’t remember exactly why I did this at the time, but it was a valid approach – it would have worked. But now I want to use physics to get proper collisions etc. and so this all needs ripping out and putting back in properly.

A quick change to the player code adds physics-based handling – but in a limited way. I’m not about to write a vehicle simulation – that would be beyond me, and unnecessary for what I still see as a simple game. So I’m cheating again. When the player accelerates I apply a force to the car that pushes them straight down the road. When the player steers I apply a force that pushes them across the road, and I also turn the car to face into the turn. This is different to what should be happening – which would be turning the car and then applying acceleration along the car’s forward axis. I could be wrong, but I think that doing it properly would cause lots of problems. My way allows me to take advantage of Unity’s physics system for collisions, but also gives me very tight control of the car. I’ve also restricted the physics quite a lot – there’s no gravity involved, and the car can’t rotate in any axis due to a collision. I may relax some of these if needed, but for now I want control. Anyway, basically I get the car running with physics quite quickly. The handling is awful, but I can tweak that later.

Next up I redo the road system, which was always a kludge. Basically the road is made up of a number of “tiles” that move towards the player. As the nearest one moves out of camera, I simply moved it to the back of the queue, and the whole thing just continued, looking like the player was moving, but they’re never actually getting anywhere. The change is a simple one – decouple it from the player’s position and move the piece further away from them, so that as they move down the road there is always a new bit to encounter. It’s still a kludge but it will work.

The final piece to getting this working is to change the camera so that its position is tied to the player’s car, rather than just floating in space (without this the player’s car zooms off out of view quite quickly). With that sorted we’re almost back where we started I just need to deal with the other cars.

At this point I added a traffic manager script to start controlling the way cars are spawned. I’ll go into this later because it’s complicated, but for now I’ll just say I had a couple of ideas about how this would work, and I’ve already realised they won’t work at all. So it goes. For now it just chucks cars along each lane at a different speed at 2 second intervals. I also added oncoming traffic on the other side of the road at this point. I then tweaked the cars to add physics to them, and then unleashed them.

Phew! A lot of text to get through, but that leaves us with things being done a little more correctly, physics is enabled so the player can collide with stuff, and there’s a script firing lots of cars with no AI at all down the road at varying speeds. Which results in this:

Now that looks like there’s proper potential to be a “Road Bastard” there to me. It’s actually quite good fun just driving along nudging other cars off course and seeing the results, which given that it’s all so basic is quite encouraging. One of the first things I’m going to do next is add an explosion for major car-to-car collisions to start getting a bit of feedback in there. Then I need to probably look at creating better traffic patterns, or maybe add some car AI.

One thing I need to think about though is how to keep as much as the action on-screen as possible. Because the player is driving fast (and at the moment nowhere near as fast as I’d like) there’s the potential for a lot of the chaos created to be lost as the player zooms past as it’s happening. I need to try to make as much happen further down the road as I can so that the player a) sees it, and b) has to deal with it.