Skip to content

About

Autonomous four-wheeled robot in Unity (C#): raycast sensors, reactive obstacle avoidance, road following and slope handling.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Repository files navigation

🤖 Autonomous Robot Navigation in Unity

Sensor-driven obstacle avoidance and road-following for a four-wheeled robot, built with Unity physics and C#

Unity C# Module

Robot driving autonomously around the track

▶️ Full-length run: 23106367.mp4 (download to watch)


📌 Overview

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.

✨ Features

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

🧠 Control loop

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
Loading

Sensor layout

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)

Tunable parameters (RobotController.cs)

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

🗂 Repository structure

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

▶️ Run it

  1. Create or open a Unity 3D project with a road scene and a robot GameObject that has a Rigidbody and four WheelColliders.
  2. Copy RobotController.cs into Assets/ and attach it to the robot.
  3. 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.
  4. Press Play and watch the robot drive.

🛠 Tech stack

Unity · C# · Unity Physics (Rigidbody, WheelCollider, Physics.Raycast) · Visual Studio

🔭 Possible extensions

  • 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.

👤 Author

S M Mahamudul Hasan — MSc Artificial Intelligence and Robotics, University of Hertfordshire · module Artificial Life with Robotics (7COM1032)

Portfolio LinkedIn

About

Autonomous four-wheeled robot in Unity (C#): raycast sensors, reactive obstacle avoidance, road following and slope handling.

Topics

Resources

Stars

1 star

Watchers

1 watching

Forks

Releases

Packages

Used by

Contributors

Languages