This repository contains a Unity-based car simulation system, including a car controller and state management for both player and bot driving modes. The system is designed with modularity and flexibility, so it can be expanded based on any car project requirements.
- INIT: Initialization state for setup.
- PLAYER_DRIVING: State where the player controls the car using input axes.
- BOT_DRIVING: AI-controlled state where the car follows a predefined spline.
- COUNT: Represents the total number of states.
- FORWARD_WHEEL_DRIVE: Power is applied to the front wheels.
- BACK_WHEEL_DRIVE: Power is applied to the back wheels.
The main CarController class is responsible for:
- Switching car states.
- Applying physics for movement, acceleration, and braking.
- Interacting with the car's
Tyrecomponents for realistic suspension and steering. - Supporting both player and bot driving modes.
- Allows switching car data at runtime, enabling different car behaviors as per requirements.
The CarData class holds the configuration for different car behaviors, allowing for easy switching of car characteristics at runtime. You can define the car's type, suspension, engine performance, and other parameters to customize how the car behaves in the simulation. This feature allows dynamic adjustments of car behavior based on project needs, such as switching between different types of vehicles or modifying a car's characteristics during gameplay.
- Car Type: Defines the type of the car (e.g.,
SPORTS_CAR,SUV,TRUCK). - Drive Configuration: Defines whether the car uses front or rear-wheel drive.
- Suspension Settings: Configures suspension force, damping, and rest length.
- Engine Performance: Sets maximum torque, speed, and acceleration rate.
- Braking: Defines braking force for deceleration.
- Steering: Specifies steering angle and speed.
- Weight and Physics: Configures mass and center of mass.
Manages car physics, states, and behaviors. Key components:
- Fields:
m_frontTyres,m_backTyres: Arrays holding tyre components for front and rear wheels.m_carDatas: List of car configurations (CarData).m_splineComputer: Spline for bot navigation.
- Methods:
InitCarStates(): Initializes and registers car states in the state machine.SwitchCarData(): Switches car configuration at runtime based onCarDataType, allowing dynamic behavior changes.AccelerateCar(): Applies torque to wheels.ApplyBrakes(): Applies braking forces to wheels.
Handles individual tyre physics, including suspension, braking, and torque application.
- Key Features:
- Suspension force calculation.
- Steering and torque application.
- Rotation simulation for visual realism.
Defines the base structure for all car states. Implements the IState interface.
Manages the player-controlled car behavior using Unity's input system.
Key Features:
- Reads input axes (
HorizontalandVertical). - Controls acceleration, braking, and tyre rotation.
Manages bot-controlled car behavior using a spline path.
Key Features:
- Follows a predefined
SplineComputer. - Adjusts tyre rotation and movement direction based on spline targets.
A flexible state machine implementation for managing car states.
Key Features:
- Supports transitions between states.
- Executes
OnEnter,OnUpdate,OnFixedUpdate, andOnExitfor each state.
-
Initialization:
- The
CarControllerinitializes with a specific state (PLAYER_DRIVINGorBOT_DRIVING). - Configurations are loaded from
CarData.
- The
-
Player Driving:
- Input controls (
WASDor arrow keys) manage car movement. - Tyres rotate and apply torque based on input.
- Input controls (
-
Bot Driving:
- The bot car follows a spline path.
- The bot adjusts steering and acceleration to reach target points on the spline.
-
Suspension and Braking:
- Physics-based suspension adjusts tyre positions and applies forces dynamically.
- Braking applies deceleration forces proportional to configured brake factors.
- Add the
CarControllerscript to your car GameObject. - Assign required components:
Tyresfor front and rear wheels.Rigidbodyfor physics.SplineComputerfor bot driving.
- Configure
CarDatafor different car behaviors. - Adjust inspector values like
Center of Mass Offset,Brake Factors, andWheel Rotation Speed.
- Gizmos: Draw suspension forces, spline targets, and tyre velocities in the editor.
- Unity Editor Handles: Display directional forces for tyres in real-time.
- Add more car states (e.g., drifting, stunts).
- Implement dynamic weather effects.
- Integrate multiplayer driving.
- Extend
CarDatafor more car types and advanced tuning options.
- Dreamteck Splines package for spline handling.
This tool represents my individual development efforts but thrives on community engagement. Feedback, suggestions, and collaborative contributions are highly encouraged. If you're passionate about mesh deformation techniques, Unity development workflows, or procedural generation, I’d love to connect!
You can connect with me on LinkedIn: Bhawesh Agarwal. Feel free to reach out if you're interested in discussing the game's mechanics, and development process, or if you simply want to talk about game design and development.
