Sensor-driven obstacle avoidance and road-following for a four-wheeled robot, built with Unity physics and C#
A rule-based autonomous agent that drives a simulated four-wheeled robot around a road course. Eight raycast "range-finder" sensors tell the robot where the road is and whether an obstacle is ahead; a reactive controller turns those readings into steering angles and wheel torque every physics step.
The project explores the Braitenberg / subsumption idea from artificial life: complex-looking driving behaviour emerging from a small set of simple sensor → motor rules, with no map and no path planner.
| Capability | How it works |
|---|---|
| Road following | Downward-angled raycasts check that the surface ahead and to each side is still road |
| Obstacle avoidance | Front and side sensors detect objects; the robot steers towards the clear side |
| Speed governing | Torque is applied only below maxSpeed; above it the robot brakes by reversing torque |
| Hill climbing | A torque boost is applied while moving forward so the robot can climb slopes |
| Realistic physics | Unity Rigidbody + four WheelColliders with visual wheel meshes synced each frame |
flowchart TD
A[FixedUpdate] --> B{Front sensor<br/>sees road?}
B -- yes --> C{Left clear?}
C -- no --> R1[Steer right]
C -- yes --> D{Right clear?}
D -- no --> L1[Steer left]
D -- yes --> S[Go straight]
R1 --> M[Move forward<br/>torque + speed limit]
L1 --> M
S --> M
B -- no --> E{Road on left and<br/>no obstacle there?}
E -- yes --> L2[Steer left]
E -- no --> F{Road on right and<br/>no obstacle there?}
F -- yes --> R2[Steer right]
F -- no --> X[Stop / recover]
M --> W[Sync wheel meshes]
L2 --> W
R2 --> W
X --> W
| Sensor | Role |
|---|---|
FRS |
Front — is the road ahead clear? |
L1S, L2S, L3S |
Left fan — road edge and obstacles on the left |
R1S, R2S, R3S |
Right fan — road edge and obstacles on the right |
ORS |
Side obstacle sensor (shorter range) |
| Parameter | Default | Meaning |
|---|---|---|
sensorRange |
30 | Raycast length for road / obstacle detection |
ORSRange |
20 | Range of the side obstacle sensor |
motorTorque |
50 | Base torque applied to all four wheels |
maxSteeringAngle |
30° | Front-wheel steering limit |
maxSpeed |
10 | Speed cap before braking kicks in |
| boost torque | 800 | Extra torque while driving forward (slopes) |
| sensor tilt | 2–4° | Downward angle so sensors read the road surface |
Robotic-Simulation/
├── RobotController.cs # sensing, decision rules and motor control
├── Assembly-CSharp.csproj # Unity-generated C# project
├── A-Life - CW2 - v2.sln # Visual Studio solution
├── converted_video.gif # demo clip shown above
└── 23106367.mp4 # full simulation recording
- Create or open a Unity 3D project with a road scene and a robot GameObject that has a
Rigidbodyand fourWheelColliders. - Copy
RobotController.csintoAssets/and attach it to the robot. - In the Inspector, assign the four wheel colliders (
FLC,FRC,RLC,RRC), their visual transforms (FLT,FRT,RLT,RRT) and the sensor transforms listed above. - Press Play and watch the robot drive.
Unity · C# · Unity Physics (Rigidbody, WheelCollider, Physics.Raycast) · Visual Studio
- Replace the hand-written rules with a learned policy (e.g. Unity ML-Agents / PPO).
- PID control on steering to reduce oscillation on straights.
- Log sensor readings to evaluate lap time, collisions and off-road events.
S M Mahamudul Hasan — MSc Artificial Intelligence and Robotics, University of Hertfordshire · module Artificial Life with Robotics (7COM1032)