This project is a Unity simulation of a basic differential drive robot navigating a randomly generated terrain. The simulation includes physics-based movement, stability control, terrain generation, and boundary walls to prevent the robot from leaving the simulation area. The robot can be controlled using keyboard inputs, and it displays real-time information such as position, speed, pitch, yaw, and roll.
- Differential Drive Robot Simulation: Simulate a robot with forward/backward movement and rotation using keyboard inputs.
- Physics-Based Interaction: The robot interacts with the terrain and obstacles realistically, including collision detection and gravity.
- Stability Control: Monitors the robot's pitch, yaw, and roll to prevent flipping and handle flips when they occur.
- Random Terrain Generation: Generates a new, gently rolling terrain each time the simulation starts.
- Automatic Robot Positioning: Ensures the robot starts above the terrain surface to prevent falling through.
- Boundary Walls: Adds walls around the terrain to prevent the robot from leaving the simulation area.
- Real-Time Debug Display: Shows the robot's position, speed, pitch, yaw, and roll on the screen during the simulation.
- Unity: Version 2019.4 or later is recommended.
- Basic Knowledge of Unity: Familiarity with the Unity Editor and basic concepts like GameObjects, Components, and scripting in C#.
-
Clone or Download the Project Repository:
git clone https://github.com/yourusername/robot-simulation-unity.git
-
Open the Project in Unity:
- Launch Unity Hub.
- Click on Add and navigate to the cloned project folder.
- Select the project and open it.
-
Set Up the Scene:
- Open the main scene (e.g.,
Assets/Scenes/MainScene.unity). - Ensure all scripts and assets are imported correctly.
- Open the main scene (e.g.,
-
Configure the Terrain:
- The terrain should already be set up with the necessary components and scripts attached.
- If not, follow the instructions in the Scripts Overview section.
Purpose: Controls the robot's movement, handles stability logic, and displays real-time information on the screen.
Key Features:
- Movement Control: Uses keyboard inputs to move and rotate the robot.
- Stability Monitoring: Checks pitch, yaw, and roll to detect instability.
- Flip Handling: Stops movement if the robot flips and provides options to reset or await self-correction.
- On-Screen Display: Shows position, speed, pitch, yaw, and roll.
Setup:
-
Attach the Script:
- Attach
RobotController.csto the robot GameObject.
- Attach
-
Assign References:
- In the Inspector, assign the
Terrainobject to theterrainfield.
- In the Inspector, assign the
-
Configure Parameters:
- Adjust
moveSpeed,turnSpeed, andstabilityThresholdas needed.
- Adjust
Purpose: Generates a random terrain with gentle slopes every time the simulation starts.
Key Features:
- Perlin Noise Generation: Creates natural-looking terrain using Perlin noise.
- Smoothing Function: Applies smoothing to reduce sharp edges and create rolling hills.
- Dynamic Size Adaptation: Adjusts to cover the entire terrain area.
Setup:
-
Attach the Script:
- Attach
RandomTerrainGenerator.csto an empty GameObject or the terrain itself.
- Attach
-
Assign References:
- Assign the
Terrainobject in the Inspector.
- Assign the
-
Configure Parameters:
- Adjust
terrainDepth,scale, and smoothing iterations if necessary.
- Adjust
Purpose: Positions the robot above the terrain surface at the start of the simulation.
Key Features:
- Terrain Height Sampling: Uses the terrain's height data to position the robot.
- Offset Application: Adds an offset to ensure the robot doesn't clip into the terrain.
Setup:
Note: This functionality has been integrated into the RobotController script.
- Ensure the
terrainreference is assigned in theRobotControllerscript. - The robot will automatically adjust its position in the
Start()method.
Purpose: Creates boundary walls around the terrain to prevent the robot from falling off the edges.
Key Features:
- Automatic Wall Creation: Generates walls based on the terrain's size.
- Customizable Dimensions: Allows adjustment of wall height and thickness.
- Physical Barriers: Walls have colliders to interact with the robot physically.
Setup:
-
Attach the Script:
- Attach
TerrainWallBuilder.csto an empty GameObject.
- Attach
-
Assign References:
- Assign the
Terrainobject in the Inspector.
- Assign the
-
Configure Parameters:
- Set
wallHeightandwallThicknessaccording to your needs.
- Set
- Forward/Backward:
W/Skeys - Turn Left/Right:
A/Dkeys - Reset Simulation: Click the "Restart Simulation" button when prompted.
-
Stability Monitoring:
- If the robot's pitch or roll exceeds the
stabilityThreshold, it will stop moving. - An on-screen prompt will appear if the robot flips, offering the option to reset.
- If the robot's pitch or roll exceeds the
-
Self-Correction:
- If the robot rights itself, a message will display for 10 seconds before auto-closing.
-
Terrain Generation:
- A new terrain is generated each time you start the simulation, providing varied environments.
-
Boundary Walls:
- Walls prevent the robot from leaving the terrain area, ensuring the simulation stays within bounds.
-
Adjusting Movement Speeds:
- Modify
moveSpeedandturnSpeedin theRobotControllerscript.
- Modify
-
Changing Stability Threshold:
- Adjust
stabilityThresholdto make the robot more or less sensitive to flipping.
- Adjust
-
Terrain Settings:
- In
RandomTerrainGenerator, tweakterrainDepth,scale, and the smoothing function to change terrain roughness.
- In
-
Wall Dimensions:
- In
TerrainWallBuilder, alterwallHeightandwallThicknessto adjust the size of the boundary walls.
- In
-
Visual Appearance:
- Add textures or materials to the terrain and walls to enhance visual fidelity.
Contributions are welcome! Please open an issue or submit a pull request with your improvements.
This project is licensed under the MIT License - see the LICENSE file for details.