Friday, 3 May 2013

Car Model and Implementation into Unity Car Tutorial

In this post i will be discussing the model we have obtained to use in our racing game, and the process of porting it into the Unity Car tutorial which we will be using as the basis for the driving in our game.

The car model

You'll have noticed i used the word 'obtained' when mentioning the car model, and yes it means exactly what you think, we have purchased a pre made car model for use in our game. This is not entirely due to a lack of the skills needed to create one of our own, in fact it's quite possible one of us could have made it, however it would most likely have taken far too long to do so the decision was made to save ourselves that time.


Unfortunately we lost some of that time due to the lesson we apparently had to learn about buying models online... (You can skip this part if you like as i will be having a mini rant)


Look at the model above, it looks great right? We thought so too. Sadly and infuriatingly, we were wrong. I no longer have the images to show you but, this model has no bottom. There is literally NO geometry underneath the car which, on a model that has been created and advertised as 'low-poly', staggers and bewilders me. Surely a model made in low-poly is created purely for use in a game environment? Maybe not if this model is anything to go by, but then again it could just be a lazily, poorly made model, i say this because not only did it have no bottom, this model also had some of the worst texturing i have seen. Again i have no picture but this guy basically had about 50% white space in a 2048x2048 map, so so so unefficient it actually made me angry, and for the space that was actually texturing? There were just 3 bog standard photographs of a Subaru Impreza from the front, back and side, as well as two random colour blocks and a cutout of the wheel from the side-on picture floating even more randomly in the white space than the other images. I will admit that despite the awful texture map that when placed on the model it looks quite good, quite convincing, and it's there that we were deceived.

The entire model just reeked of laziness and after spending an hour trying to obtain a refund we were granted one. So after making the note to never buy a model that doesn't show every angle in the screenshots again, we found a better, albeit slightly more expensive model.

The car model we will actually be using in our game

Here is the car model we will be using in our game, it has a bottom, has an efficient, well laid out texture map, and the slightly lower resolution of the texture map actually fits in with our retro, 90's rally racer theme. We were all very happy with this purchase.


Implementing the car model into the Unity Car Tutorial

I will be updating this section as i go along.

The first thing i noticed is that this model lacks collision meshes, it's made clear in the tutorial that these are required purely for performance reasons, but while perhaps not an issue with our low-poly car model, i thought it'd be best to make an even lower-poly, slightly blocky pair of collision meshes (one top, one bottom) that would perhaps emulate the collision detection available to those earlier games.




Another thing i noticed was that the wheels in this car were just wheels, whereas the Unity car has wheel meshes that are parented to disk brake models, as far as i understood it, however, the disk brakes were purely cosmetic and i would be able to just use the wheel meshes.

So i then took the model and all its components into Unity using the 'Import New Asset' function, and was immediately greeted by a slightly worrying list of mesh components, simply put all the individual parts of the model had been imported as individual parts, though they were grouped together. This was worrying because the Unity cars model was so much simpler by comparison and i was concerned that this would cause problems, still i went ahead with moving the car model into the scene as instructed by the tutorial and proceeded to set everything up as best i could.

After i was satisfied i had followed the instructions properly, i pressed play aaaaaaaand

Nope. No good.

It seemed the collisions for the body were fine, but for some reason the wheels were not being detected, they just clipped straight through the floor, though they were still attached. This meant the car was unable to move, so i had some bug fixing to do, it was my assumption that there were just some names that i had to match for the code to work properly. Either that or i needed to make some disk brake meshes!

Okay so i tried out adding the disc brake meshes from the default car to our car model and made the wheel meshes children of them. I pressed play aaand...


SUCCESS!...

...sort of. I can now drive the car! Though as you can see there are some teething problems for this setup, I will now make some adjustments to the cars model and re-import it to Unity. I'm simply planning to add my own disk brake meshes to the model and i may also adjust the wheel sizes a bit.

After doing just that (and taking the opportunity to improve the organisation of the model) I once again set everything up according to the tutorial. Unfortunately, despite this effort i had inadvertently taken myself a step backwards, i was once again working with a car whose wheels weren't being properly detected. On top of this, i noticed another problem, the rotations of all the wheels seemed to be off, and my attempts to manipulate them back into place (under the assumption this had something to do with the problem) caused some bizarre warping of the models.

After searching online for a solution to this problem I came across this thread on the Unity Forums that gave me the solution i needed. Apparently all i needed to do was go back to the model in 3DSMax and adjust the pivots so that the Y-axis was pointing upward, this is due to a disparity between the axes used by Unity and 3DSMax. I had some trouble with the disk brake models i had created when trying to adjust their pivots, namely some bizarre warping that was occurring. To fix this i simply had to convert said models into 'editable poly's'. Once again I exported the model and imported it into unity, with great results:


As you can see from above it appears the car is all but working perfectly! But sadly, there was one more small thing left to fix:


As you can see the rear wheels aren't quite aligned with their forward-positioned counterparts, though oddly their position resets when the car is off the ground.

After attempting to solve this alone without success, i took my problem to my classmates, and with the help of Dean Stone, found a solution. And my goodness was it a simple (if slightly inelegant) solution. It seemed i had been too lost in the assumption that the problem could be solved through manipulation of the code, i had located what i believed to be the culprit in the 'Car' script, but had no idea how to go about fixing it. So, after asking for the help of my classmate i was astounded to see that his suggestion of simply moving the wheel meshes proved successful. I hadn't considered that such a simple solution would work, and yet the proof is in the picture:


The wheels now aligned properly with the car, and while the wheels, when off the ground reverted to their slightly odd positions, though for the vast majority of playtime this is unnoticeable.

Despite not having fixed a lot of the problems with this car such as the floaty gravity, bizarre forward movement bug and the slightly buggy camera, i believe the car setup is now at a point where it is suitable for implementation!

A couple of final adjustments i make will be to the smoke effect from the tires, adjusting it to make it look like sand, in keeping with our desert track design.

Although i didn't delve into the world of coding during my time spent importing the car mesh, i am proud of what i have achieved here. Though importing was frustrating at times, it's incredibly gratifying to see your own model (albeit an outsourced one) moving around a game environment and behaving as you want it to, like a rally car. On top of this, my time spent here has only improved my respect for the simplicity that Unity provides.

No comments:

Post a Comment