Lesson 2: Environment Design
Now that your Unity project is set up, let's design the simulation environment where your RL agents will learn and interact.
๐ฏ Learning Objectivesโ
By the end of this lesson, you'll have:
- A basic 3D environment for RL simulation
- Interactive elements for agent interaction
- A foundation for reward system implementation
๐๏ธ Environment Componentsโ
Core Elementsโ
- Ground Plane: The base where agents move
- Walls: Obstacles and boundaries
- Food Sources: Collectible items for rewards
- Spawn Points: Where agents start each episode
Interactive Elementsโ
- Collectibles: Items that provide positive rewards
- Obstacles: Elements that provide negative rewards
- Goals: Target locations for agents to reach
๐ ๏ธ Step-by-Step Implementationโ
1. Create the Groundโ
- Right-click in Hierarchy
- Select
3D Object > Plane - Rename to "Ground"
- Scale to (10, 1, 10) for a larger area
- Apply a material (create a new material if needed)
2. Add Wallsโ
- Create a new GameObject:
3D Object > Cube - Rename to "Wall"
- Scale to (1, 2, 10) for a long wall
- Position at the edge of the ground
- Duplicate and position walls around the perimeter
3. Create Food Sourcesโ
- Create a new GameObject:
3D Object > Sphere - Rename to "Food"
- Scale to (0.5, 0.5, 0.5)
- Add a bright material (yellow or green)
- Create a prefab from this object
4. Set Up Spawn Pointsโ
- Create empty GameObjects
- Name them "SpawnPoint1", "SpawnPoint2", etc.
- Position them around the environment
- Add a visual indicator (small cube or sphere)
๐จ Visual Designโ
Materials and Colorsโ
- Ground: Neutral color (gray or brown)
- Walls: Dark color (dark gray or black)
- Food: Bright, attractive color (yellow or green)
- Spawn Points: Distinctive color (blue or red)
Lightingโ
- Go to
Window > Rendering > Lighting - Set Environment Lighting to "Skybox"
- Adjust Ambient Intensity to 0.3
- Add additional lights if needed
๐ง Scripting Basicsโ
Environment Manager Scriptโ
Create a new C# script called EnvironmentManager:
using UnityEngine;
public class EnvironmentManager : MonoBehaviour
{
[Header("Environment Settings")]
public Transform[] spawnPoints;
public GameObject foodPrefab;
public int foodCount = 10;
private GameObject[] foodItems;
void Start()
{
SpawnFood();
}
void SpawnFood()
{
foodItems = new GameObject[foodCount];
for (int i = 0; i < foodCount; i++)
{
Vector3 randomPos = new Vector3(
Random.Range(-4f, 4f),
0.5f,
Random.Range(-4f, 4f)
);
foodItems[i] = Instantiate(foodPrefab, randomPos, Quaternion.identity);
}
}
public void ResetEnvironment()
{
// Reset food positions
foreach (GameObject food in foodItems)
{
if (food != null)
{
Vector3 randomPos = new Vector3(
Random.Range(-4f, 4f),
0.5f,
Random.Range(-4f, 4f)
);
food.transform.position = randomPos;
food.SetActive(true);
}
}
}
}
โ Testing Your Environmentโ
- Play the scene to test the environment
- Verify food spawning works correctly
- Check lighting and visual appeal
- Test reset functionality if implemented
๐ฏ Next Stepsโ
Once your environment is ready:
- Proceed to Q-Learning Python - Next section
- Review Getting Started if needed
- Check Glossary for more concepts
๐ก Tipsโ
- Keep it simple: Start with basic shapes and add complexity later
- Use prefabs: Create reusable components
- Test frequently: Play the scene often to catch issues early
- Document decisions: Note why you made specific design choices
Excellent! Your environment is taking shape. Next, let's add the agent that will learn to navigate it!