This is a full Manual and Documentation for the Unity Any Car AI. It will cover all features, uses and set up procedures.
Set up a car ai in Unity as never been so simple and quick and it literally takes 1 minute.
The script is adaptable to any car model. Only necessary requirement is that the wheels must be separated objects from the car body and their pivot point must be on the relative wheel position.
Also if you want to use the Damage System, make sure you tick the Read/Write Enabled Check on the model. That will allow you to Deform the mesh.
- Drag and Drop the AnyCarAI Prefab into your Scene.
- Drag and Drop your car model as a children of the “ModelParent” object in the Hierarchy.
- Drag and Drop references of wheels and body into the appropriate fields of the AnyCarAI component into the ControllerScript game object.
- If your vehicle has more than 4 wheels add the number of the extra wheels you need into ExtraWheels > Size.
- Drag and Drop the references for your extra wheels models and select whether you want the wheel to be front(steering wheels) or rear and whether you want them to be drive or trail wheels.
- A consistent car body model is necessary to keep the wheels together and avoid bugs. In case you do not have that on your model PRESS the “CreateBodyCollider” Debug button and it will create a car body template.
- Press “Attach Script” to complete set up.
- Drag and Drop the WaypointsPath Prefab into your scene and move or add nodes in order to create the path for you car and make a refercence on the AI Tab under WaypointsPath
- Don’t forget to adjust the position of Sensors in order to have the necessary driveability.
Skid Marks: This option will activate skid marks sound, marks renders and smoke on the floor when the wheels friction become particularly high.
Exhaust FX: This option will create a game obbject in the hierarchy. You will have to place this game object next to the exhaust pipe of your car model. It will make a firing visual effect and a exhaust sound when you will change gear up with your car. You can control the audio volume in the audio Tab of the Any Car AI Script.
ABS: This option will activate the Anti Brake-locking System.
Smoke: This option will activate smoke coming out of the car.
If you want to use the Damage System, make sure you tick the Read/Write Enabled Check on the model before you import the model into your scene. That will allow you to Deform the mesh.
Be aware that these values really varies depending on your car model.
If you are using a low poly model for instance decimal values will be enough to have a damage effect. With a high poly model you might need much higher number. You will have to test it yourself based on your model.
Demolution Strenght: How much will your car be destroyed based on collision.
Demolution Range: How big will be the damage on your car.
Collision Particles: Will create dusty effects on collision.
Custom Mesh: For more complex models this array will allow you damage only certain parts of your car on collision. It will also allow you to lose certain parts after a number of collisions that you can set in the array.
If you leave the number to zero you will not lose that part at all.
The wheels attributes determine the actual physics of the wheels and suspensions of the car as well as drift settings.
Wheels Radius: Setting up the script the wheels will be created with the actual size of the wheels meshes. This parameter is a multiplier in case you want to adjust the wheel colliders size.
Wheels Mass: The Mass of the wheel.
Dumping Rate: This is a value of damping applied to a wheel.
Force Point: This parameter defines the point where the wheel forces will applied. When the parameter is zero the forces will be applied at the wheel base at rest.
Suspension Spring: A larger value makes the suspension reach the Target Position faster.
Suspension Dumper: Dampens the suspension velocity. A larger value makes the Suspension Spring move slower.
Suspension Distance: Maximum extension distance of wheel suspension, measured in local space. Suspension always extends downwards through the local Y-axis.
Target Position: The suspension’s rest distance along Suspension Distance. 1 maps to fully extended suspension, and 0 maps to fully compressed suspension.
Max Steer Angle: Steering angle in degrees.
Wheel Stiffness: Changes the stiffness of the friction. Setting this to zero will completely disable all friction from the wheel.
Steer Helper: Will change physics in order to make steering easier.
Traction Control: Will improve the traction of your car both on steering and accelerating.
Slip Limit: A lower value will make it easier for your car to slip.
The debug section is really only used for particular situations where the car model has no consistent pivot point and object structure and using this values you can put every bit back in the right position.
Wheel Position: It is a Vector3 Value and it determines the position of the actual wheel.
Wheel Rotation: It is a Quaterion Value and it is used in case the pivot points of your car models do not respect the directionality of the rest of the car. This can cause some strange behaviour of the wheels on runtime. This value is used to adjust this behaviour.
Car Drive Type: Front Wheels. Engine power will be applied only on front wheels. Rear Wheels. Engine power will be applied only on rear wheels. 4×4. Engine power will be distributed on all wheels.
Number of Gears: How many gears you want your car to have.
Engine Power: The value is represented with the power curve over time. It is basically how the motor Torque is distributed over time when accelerating.
Motor Torque: Motor torque on the wheel axle expressed in Newton metres.
Brake Torque: Brake torque expressed in Newton metres.
Reverse Torque: Reverse torque on the wheel axle expressed in Newton metres.
Handbrake Torque: Brake Torque only applied on rear wheels.
Down Force: This control will enhance the aerodynamics of your car giving more control on high speed.
Turbo: Activating the Turbo option will give your car a small extra power and it will enable the turbo sound (controllable from the audio tab of the Any Car AI Script).
Speed Type: Choose whether you want the speed of your car to be measured in MPH or KPH.
Max Speed: This value is based on speed type value and it will determine the maximum speed that the car can reach. Once it reaches it, the car will keep its speed still.
Vehicle Mass: It controls the actual weight of the vehicle.
Center of Mass: It is a Vector3 value and it represent the point of maximum weight. It is often used to give the car more balance on steering.
Waypoints Path Reference: Make a reference of the waypoints path that you want the AI to follow. This reference must include the waypointsPath component and its childs will create the path.
You can customize how smooth you want the path to be through the waypointsPath component.
Follow Options: You can either choose to smoothly follow the path or make the car go towards each waypoint in the path.
Sensors Angle: How wide in front of the car will the sensors work.
Avoid Distance: It determine how close to an obstacle your car needs to be to calculate a new path.
Brake Distance: Here select how close your car need to drive to an Obstacle in order to brake.
Reverese Distance: Here select how close your car need to drive to an Obstacle in order to use Reverse Gear.
All controls will make Accelerating, Steering, Racing and Braking more human.
Control how aggressive is each action using sensitivity and wander.
You can also choose when your car should brake along the path.
Persuit AI: make a reference of the target gameObject to follow and how far it needs to be to start following it.
Choose Audio Clips and volume for the Audio Fx.
The Engine Sound is a 4 channel Audio Effect. Choose sounds that you can loop and that simulate high and low RPM in accelerating and decelerating.
If you enable other functions in the Any Car AI Script you will find other channels to control, such as:
- Exhaust Audio
- Skid Marks Audio
- Suspensions Audio
- Turbo Audio
- Collision Audio
The Any Car AI Prefab contains an AIScript Object where all the magic happen.
Here you will find the script used to make references and to control physics and features.
This object contains 4 Childs:
- WheelsParent: Once the script is attached it will contain the wheel colliders.
- Utility: Contains Particles System, Front Lights, Rear Lights, Sensors and if exhaust FX is on Exhaust Object
- ModelParent: Use this object as parent of your car model.
Hope you enjoyed. Feel free to contact me if you have any question, request or doubt.
Thanks for your attention on Any Car AI (Car Physics) for Unity.
— BoolBird —