查看: 1765|回复: 0
打印 上一主题 下一主题

国外的一篇赛车教程(上)

[复制链接]

5552

主题

2

听众

8万

积分

首席设计师

Rank: 8Rank: 8

纳金币
-1
精华
11

最佳新人 活跃会员 热心会员 灌水之王 突出贡献

跳转到指定楼层
楼主
发表于 2012-1-31 12:03:59 |只看该作者 |倒序浏览
For a long time there have been topics on the unity3d Forum asking about how to set up a car rig, there don’t seem to be many good tutorials out there, so I decided to write one of my own that will hopefully explain some of it for you.
Step 1 : Importing the Game Object.
The first step in creating a car setup is to set up the model in the editor so that it contains all of the necessary components. When you create a car model in your 3D modeling program (I’m using maya), you want to make sure that you have the body, and all four wheels of the car modeled and loaded into Unity. You can have extra components, but it becomes increasingly confusing the more objects you’re dealing with. I would only recommend having more if you were building a game that required the car to have break away panels or some sort of damage system.

Here you can see the basic setup I’m using. I grouped all of the wheels inside of one object so that I can easily hide them in the hierarchy.

Step 2 : Attaching the Physics Components
First, attach a rigidbody to your car object. click on Component/Physics/Rigidbody in the menu. This will make the car model a rigidbody, making it a part of the Unity physics environment. In order to have it properly react to collisions and behave like a car, we need to attach the colliders.
I generally create an empty game object, and make sure that the position and rotation are set to (0,0,0). Rename this “Colliders”, this will serve as a container for all of the colliders that will be applied to the car. Then, create a few game objects and position them inside of this object. Then select them and click Component/Physics/BoxCollider in the menu. This will allow you to approximate the general shape of the car, while still keeping it simple.
NOTE: I generally create colliders as separate child objects of the main body, this way I can have multiple colliders in the general shape of the car. You could use a mesh collider for this, but mesh colliders do not work well for objects moving at high speeds.

In order to make the car behave properly, you can use the Unity3d built in Wheel Collider components. These are essentially extensions of the physics engine that use a raycast to determine the distance to the ground, then they apply a force on the rigidbody based on the compression of the “spring”.
Create another empty game object, just like you did for the colliders, and name this “WheelColliders”. You could put this in the “Colliders” object, but I find it easier to organize them separately. Next, place four empty objects in this container and select Component/Physics/WheelCollider in the menu. This will attach wheel colliders that can be configured to behave like real wheels. Position these colliders so that they are about where they should be on your car. They are invisible, so you can put them in slightly different places, but try to get them as accurate as possible.


NOTE : Wheel Colliders have a property called “Radius” as well as one called “Suspension Distance”, this determines the radius and spring length of the virtual wheel in the physics engine. You will need to adjust these property so that it closely matches the actual radius and suspension distance of the wheel in your model.

Step 3 : Configuring the Physics Setup
Now that you have all of the parts set up, you need to tweak them so they work together properly. I generally model my vehicles off of existing real world cars. This is a safe way to ensure that your car behaves relatively well.
There are three key factors you will need to set.
■the vehicle mass

■the suspension spring and damping

■the tire friction


Starting with the simplest of the three, vehicle mass. I generally set the mass of my vehicles to a value around 1000-2000 kilograms. Depending on how you want your car to behave. You can use any value you like, but if you’re going for a realistic setup, I would recommend staying within these values. To set the mass, select your base car object, and look in the inspector. There should be a tab for “Rigidbody”, inside of this tab, there is a property called mass. Enter the chosen value, and press enter. Remember, all units in the physics engine are in Metric, so this value will be in kilograms.
NOTE : if you would like to convert English units to Metric, simply type it into Google and Google Calculator will automatically convert it for you.
Now to configure the wheel colliders. You will need to do this to all of the colliders, so select them one at a time and enter the values you like.
The suspension spring tab will let you set the spring values. These are important for making your car suspension behave in a realistic manner, and not just throwing your car across the map. Depending on the mass of your car, you will need to adjust the spring. Generally a value between 2000-5000 will do for a medium-weight vehicle as long as you set the damping value properly. Suspension damping slows the rate of spring extension. It prevents the springs from oscillating back and forth. I used a value of 50 in this example, but you may need more if the length of the suspension is set longer.
Then you need to tweak the forward and sideways friction of the wheel. Forward friction determines the amount of friction on the wheel when rolling in the direction it’s facing, Sideways friction determines the amount of friction that prevents the wheel from sliding sideways. If you would like to know more about how these values work, look them up in the Unity Reference Manual here
http://unity3d.com/support/documentation/Components/class-WheelCollider.html
I would not recommend changing the asymptote values until you know exactly how they work, it’s much easier to change the “Stiffness” value. This is essentially a multiplier for the curve, when set lower, it decreases both values linearly. You will want this value set fairly low, generally less than 0.1 if you want your car to slide nicely. If the sideways friction stiffness is set too high, you may have some stability issues, the car will stick to the road so much, that when you try to turn at high speeds, your car will quite literally roll off into space.

Step 4 : Writing a Control Script
Now that we have the car set up in the editor, we can proceed to write the script to control it. Because I want this car to behave like a real race car, the script uses a system to simulate different gears, Each with a different gear ratio.
Unity wheel colliders are simple to use, and allow the user to easily set an engine torque value to power the wheels, but how do we know what to set this value to?
What I do, is specify a maximum engine Torque, maximum and minimum engine RPM, and an array of gear ratios. A gear ratio specifies how many rotations the engine makes, per rotation of the wheel. Simply put, it allows the wheels to spin at higher and higher speeds while still maintaining a constant engine RPM. The car must switch to a higher gear ( a gear with a lower ratio ) to maintain speed without the engine reaching it’s maximum possible speed.
So, because we have the wheel RPM ( it’s given by the wheel collider objects ), we can determine the engine RPM by multiplying the wheel speed by the ratio of the current gear. Using this we can determine what gear the engine should be in.
■If the calculated engine speed is greater than the maximum engine RPM, loop through all of the gears, checking to see whether the engine RPM multiplied by that gear ratio is less than the maximum engine RPM, if it is, then switch to that gear. This is essentially saying, if the engine is going too fast, make it so it isn’t.

■The same goes for downshifting, if the engine is going too slow, loop through all of the gears from highest to lowest, checking if it’s within the acceptable engine range.

Let’s review. Now we have a value for the engine RPM, and our script will automatically switch gears if it is higher or lower than it should be. Now we just need to apply a torque to the wheels depending on the gear.
The torque applied to the wheels should be the maximum engine torque divided by the gear ratio of the current gear. This will make it so that the torque applied by the engine increases as you shift up, and decreases as you shift down.
In order to make the car controlled by the player, just multiply the torque on the wheels by the input value, if you use “Input.GetAxis()”, it will work perfectly. To make the car steer, you can just set the steer angle of the wheels to a base value multiplied by the input value.
分享到: QQ好友和群QQ好友和群 腾讯微博腾讯微博 腾讯朋友腾讯朋友 微信微信
转播转播0 分享淘帖0 收藏收藏0 支持支持0 反对反对0
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

关闭

站长推荐上一条 /1 下一条

手机版|纳金网 ( 闽ICP备08008928号

GMT+8, 2024-5-6 02:23 , Processed in 0.083855 second(s), 32 queries .

Powered by Discuz!-创意设计 X2.5

© 2008-2019 Narkii Inc.

回顶部