FatNick Makes a Game: Part 2

(Part 1)

So last time on Fatnick Makes a Game we looked at how to fire up Unity, create a cube and some spheres, then sit and stare at the screen wondering why they won’t spontaneously form into a usable car.

This time we’re going to get that car moving!

When the car failed to move last time, I was faced with two choices: stick with the script I had initially and fix it, or try and find/build a new one. Thankfully, someone had put together a simpler car controller script, so I was lazy and switched over to that instead. Ha!

Once the script had been copied and assigned to my 4 wheel colliders…the car still didn’t move. Argh. What could be wrong? Was there still time to back out and pretend that I’d never put any real effort into making a game?  But then I had my breakthrough: “Hang on!what’s all this “input.getaxis” malarky?”

To cut a long story short, it turns out that by default the control axis was being mapped to the embarrassingly obvious W, S, A, D keys. This, ladies and Gentlemen, is why you should always, always R.T.F.M.

 

make-game

Of course, though it was now controllable, the ‘car’ still didn’t behave like a car. Give a bit too much power to the wheels and it would take off into a somersault. Steer too harshly and it would launch into an almost endless barrel roll. Wheee.

But the good news is that this sort of makes sense. You see, the reason your car doesn’t somersault is because it’s got plenty of weight (you, the seats, the dashboard, a great big whopping engine) keeping it on the ground, with (inevitably expensive) suspension components helping to keep any lateral movement in check. My car had lots of power to relatively little weight, which is why it was essentially uncontrollable. There’s a reason that F1 cars are designed the way they are, it seems.

Hitting Unity forums, blog posts and other free and remarkably thorough resources, I started to crib together the bits I needed to make the car more manageable. In particular, I lowered the torque from my original estimate, and inserted a line of Javascript to artificially lower the center of gravity. Is this cheating? Yes, I suppose it was a bit! but at least it’s rendered my crazy box car into a drivable box car. I’ll sort the physics out properly later – once the other bits and pieces I need to model the car are in place.

make-game

Alongside the physics corrections (and inserting an EXCITING reverse gear)  I also worked a bit on the cosmetics. My box car is never going to be a pretty, realistic-looking automobile, but at the very least its’ wheels should roll when driving and pivot when steering. The good news is the code to get these two aspects working is reasonably straightforward. The bad news is that, for some reason, the rolling code appears to have been applied to completely the wrong axis.

At first, the wheels flopped feebly to the floor. Then, after a bit of tweaking, they spun laterally, like a coin standing on its edge. This is one to look at with a fresh pair of eyes, i think.

Overall then, not a bad couple of hours progress. Not only can the car move forward and reverse, but when it steers the front wheels point in the right direction. It can even take jumps with a degree of realism. Well, until you try and steer, that is:

Look! It can jump properly!

So then, what next? Well, I have a feeling the wheel alignment might be off – which probably isn’t helping the sideways stability. I want to address that, get my wheels rolling forward and then look into creating some sort of artificial anti roll bar. The stunt track might have to wait for a little bit, but we’ll get there!

 

One Reply to “FatNick Makes a Game: Part 2”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.