Game programming

My first game in Unity part2 – My first working car in 3D with 2D physics!

Hola muchacho!

This is the second part of my first steps in Unity.
After my first not-amazing-but-at-least-it-kinda-worked car in 3D physics, and because I felt that I wouldn’t be able to make my little game using those physics, I wanted to try it using 2D physics. So again, tutorials, struggles aaaaand… more struggles. I find that the tutorials here and there are not complete. For example, I couldn’t find one that would talk about how to set brakes in a “realistic” way (actually I couldn’t find anything relevant which means that I suck at searching info x_x )

My process
Like many, I watch tutorials and follow each step to understand and learn. Needless to say it is a must, and I’ll never thank enough all of those who share on the web.
So I used provided sprites, typed several codes (as there are several tutorials about the same subject, but not everyone does things the same way, which is very interesting), but in the end wasn’t satisfied with the result. I still am not.
Having played Prime Peaks quite a lot, I figured that the game was made as a 2D game using 3D objects. But even though everything seems to be having perspective, all of that happens in two dimensions, and whatever 3D you see if only for visual purpose only: the collisions only happen on the xy plan, the perspective that one seems to be seeing it not taking into consideration. So after having fused a few neurons, I thought that with some tweaking, even 4 wheels could be behaving like 2 wheels in a 2D view (actually a car still has 4 wheels, but in side view, one can only see 2, and in-game there indeed are only 2 wheels).
Let me show you how I managed that:

You can see 3 pictures.
   1) Inside the car, you can see green wires. One has the shape of the car, it’s what represents the collision mesh, or in other terms, how the game sees the shape of my car, and where things are going physically interact with.
The green circles are the wheels colliders, called Wheel Joint 2D (including the springs and all the parameters related to the wheels behavior such as the motor).
   2) You’ll have probably noticed that there are 2 green circles for 4 wheels. As I just wrote, that’s because it’s a game in 2 dimensions. Being that so, the rear wheels behave like ONE rear wheel, and the front wheels behave like ONE front wheel (and obviously, there is no left/right turn). The solution is to arrange the hierarchy so that the wheels are grouped together accordingly, and that the group will act as a wheel. So first I selected a wheel, say a front wheel, then I created an empty object. When you select an object and create an empty object, the latter will have the same coordinates as the parent object you just selected, which is very important in that case, because the empty object will rotate and have the 2 children wheels rotate at the same time. Any difference would result in an ugly spinning. After that, I moved the empty object ( without forgetting beforehand to set its x coordinates to 0, which means it should be placed right between the 2 wheels – note that I’m not sure if it’s really necessary, but I like when it looks clean and logical) and made it become the parent of the wheels that I just used to create that empty object.
I did that for both pairs of wheels and renamed each empty object that contains the wheels respectively RearWheels and FrontWheels (nevermind the “Hidden”, I don’t know why I wrote that, but anyway you can simply change the name, which is what I did, so no bother).
Important: those 2 empty objects are to be used in the corresponding Wheel Joint 2D.
   3) The script. It shows that we can use, as you had guessed, 2 (groups of) wheels, hence my previous steps.

At that point, I had a running car, but the controls were not what I was expecting: there was no braking. Pressing the up-arrow key would accelerate, pressing the down-arrow key would reverse the engine (going from speed value A to speed value -A directly or so…).

I had to find a way to brake! But unfortunately I haven’t been able to find anything (yes, even at the time I wrote that post). So what did I do? You’ll see my first (far from being perfect) solution in the next episode…

Leave a Reply