CarController Class Documentation
Overview
The CarController class is responsible for controlling a car's movement, including steering, acceleration, and braking. It interacts directly with Unity’s WheelCollider and Transform components to simulate realistic car physics and update the visual position and rotation of the wheels based on player input.
This script is designed to be beginner-friendly, easy to customize, and suitable for both personal and commercial Unity projects.
🔓 Public Variables
⚙️ Settings
-
motorForce (float)
The force applied to the car’s motor for acceleration.
Higher values result in faster acceleration. -
breakForce (float)
The force applied when the car is braking.
Higher values result in stronger and quicker stopping. -
maxSteerAngle (float)
The maximum steering angle of the front wheels.
Higher values allow sharper turns.
🛞 Wheel Colliders
-
frontLeftWheelCollider (WheelCollider)
WheelCollider for the front-left wheel. -
frontRightWheelCollider (WheelCollider)
WheelCollider for the front-right wheel. -
rearLeftWheelCollider (WheelCollider)
WheelCollider for the rear-left wheel. -
rearRightWheelCollider (WheelCollider)
WheelCollider for the rear-right wheel.
These components handle suspension, friction, and collision physics.
🎨 Wheel Transforms (Visuals)
-
frontLeftWheelTransform (Transform)
Visual transform of the front-left wheel. -
frontRightWheelTransform (Transform)
Visual transform of the front-right wheel. -
rearLeftWheelTransform (Transform)
Visual transform of the rear-left wheel. -
rearRightWheelTransform (Transform)
Visual transform of the rear-right wheel.
These transforms are updated every physics frame to match the WheelCollider’s position and rotation.
🔒 Private Variables
-
horizontalInput (float)
Stores player steering input (left / right). -
verticalInput (float)
Stores player acceleration input (forward / reverse). -
currentSteerAngle (float)
Current steering angle applied to the front wheels. -
currentBreakForce (float)
Current braking force applied to all wheels. -
isBreaking (bool)
Indicates whether the brake key (Spacebar) is pressed.
🧠 Methods
FixedUpdate()
The main physics update loop, called every physics frame.
It performs the following actions in order:
GetInput()HandleMotor()HandleSteering()UpdateWheels()
GetInput()
Collects player input:
- Horizontal axis (
Input.GetAxis("Horizontal")) for steering - Vertical axis (
Input.GetAxis("Vertical")) for acceleration and reverse - Spacebar (
Input.GetKey(KeyCode.Space)) for braking
HandleMotor()
Applies motor torque to the driven wheels based on vertical input.
- Applies acceleration or reverse torque
- Checks if braking is active
- Calls
ApplyBreaking()when needed
ApplyBreaking()
Applies brake torque to all WheelColliders when braking is active.
- Brake strength is controlled by
currentBreakForce - Ensures consistent stopping behavior across all wheels
HandleSteering()
Adjusts the steering angle of the front wheels based on horizontal input.
- Steering angle is clamped using
maxSteerAngle - Provides smooth and responsive turning
UpdateWheels()
Updates the visual position and rotation of each wheel by calling:
UpdateSingleWheel()for every wheel
UpdateSingleWheel(WheelCollider wheelCollider, Transform wheelTransform)
Synchronizes the visual wheel with its physics collider.
- Retrieves wheel position and rotation using
GetWorldPose() - Applies them to the corresponding Transform
This ensures accurate visual feedback matching the physics simulation.
📝 Notes & Best Practices
- Ensure all WheelCollider components are properly aligned with their visual wheel meshes.
- Terrain or road surfaces must have a Collider to prevent wheels from sinking or floating.
- This script uses Unity’s default Input Manager axes, making it compatible with:
- Arrow keys
- WASD
- Gamepads / joysticks
- The controller is designed as a front-wheel-drive system.
- For rear-wheel or all-wheel drive, apply motor torque to additional WheelColliders.
💖 Support the Creator
If you have got even 1 dollar to support, please do:
https://www.krishnamohanproductions.com/donate