0% found this document useful (0 votes)
3 views5 pages

Simulation Script

The simulation script manages the initialization of foxes, rabbits, and grass, while dynamically adding grass and collecting population data. It tracks and logs the counts and average speeds of the animals, updating a line chart for real-time visualization. Key concepts include resource management, data collection, and visualization of population trends.

Uploaded by

sid.souhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views5 pages

Simulation Script

The simulation script manages the initialization of foxes, rabbits, and grass, while dynamically adding grass and collecting population data. It tracks and logs the counts and average speeds of the animals, updating a line chart for real-time visualization. Key concepts include resource management, data collection, and visualization of population trends.

Uploaded by

sid.souhil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Simulation Purpose

The Simulation script manages:

1. Initialization: Spawning the initial population of foxes, rabbits, and grass.


2. Dynamic Grass Growth: Adding grass to the scene at random intervals.
3. Data Collection: Tracking and logging the population counts and average speeds of
foxes and rabbits over time.
4. Visualization: Updating a line chart with population data for real-time monitoring.

Step-by-Step Guide

1. Variables and Initialization


csharp
Copy code
public float start_rabbit; // Initial number of rabbits to spawn.
public float start_fox; // Initial number of foxes to spawn.
public float grass_spawn_rate; // Probability of spawning new grass
each frame.
public float rabbit_min_speed; // Minimum speed for rabbits.
public float rabbit_max_speed; // Maximum speed for rabbits.
public float fox_min_speed; // Minimum speed for foxes.
public float fox_max_speed; // Maximum speed for foxes.

public GameObject Rabbit; // Reference to the Rabbit prefab.


public GameObject Fox; // Reference to the Fox prefab.
public GameObject Grass; // Reference to the Grass prefab.

public LineChart lineChart; // Reference to the LineChart for


visualization.
private float time; // Timer for tracking intervals.

public List<int> foxes = new List<int>(); // List to store fox


population counts.
public List<int> rabbits = new List<int>(); // List to store rabbit
population counts.
public List<float> fox_speed = new List<float>(); // List to store average
fox speed.
public List<float> rabbit_speed = new List<float>(); // List to store average
rabbit speed.

public int rabbitnumber, foxnumber; // Current population counts for rabbits


and foxes.

Explanation:
• These variables define key parameters for the simulation (e.g., population sizes, grass
growth rate, and speed ranges for animals).
• Lists are used to store data for analysis or visualization.

2. Start() Method

This method initializes the simulation by spawning the starting populations of foxes, rabbits, and
grass.

2.1 Clear Existing Data

csharp
Copy code
lineChart.ClearData(); // Clear any previous data from the chart.

2.2 Find Prefabs

csharp
Copy code
Rabbit = GameObject.Find("Rabbit");
Fox = GameObject.Find("Fox");
Grass = GameObject.Find("Grass");

Purpose:

• Fetches the references to the Rabbit, Fox, and Grass prefabs.

2.3 Spawn Initial Foxes

csharp
Copy code
for (int i = 0; i < start_fox; i++) {
Instantiate(Fox, new Vector3(Random.Range(-50f, 50f), 1, Random.Range(-
50f, 50f)), Quaternion.Euler(0, Random.Range(0f, 360f), 0));
float speed = Random.Range(fox_min_speed, fox_max_speed); // Randomize
speed.
Fox.GetComponent<Fox>().speed = speed; // Assign speed.
Fox.GetComponent<Fox>().energy = Random.Range(0f, 100f); // Assign random
energy.
}

Explanation:
• Spawns start_fox foxes at random positions within a 100x100 area.
• Each fox is given a random speed and energy level within the defined ranges.

2.4 Spawn Initial Rabbits

csharp
Copy code
for (int i = 0; i < start_rabbit; i++) {
Instantiate(Rabbit, new Vector3(Random.Range(-50f, 50f), 1,
Random.Range(-50f, 50f)), Quaternion.Euler(0, Random.Range(0f, 360f), 0));
float speed = Random.Range(rabbit_min_speed, rabbit_max_speed); //
Randomize speed.
Rabbit.GetComponent<Rabbit>().speed = speed; // Assign
speed.
Rabbit.GetComponent<Rabbit>().energy = Random.Range(0f, 100f); // Assign
random energy.
}

Explanation:

• Spawns start_rabbit rabbits in a similar way to foxes, randomizing their positions,


speeds, and energy levels.

2.5 Spawn Initial Grass

csharp
Copy code
for (int i = 0; i < 20; i++) {
Instantiate(Grass, new Vector3(Random.Range(-50f, 50f), 1, Random.Range(-
50f, 50f)), Quaternion.identity);
}

Explanation:

• Spawns 20 grass objects randomly across the 100x100 area.

3. Update() Method

This method handles grass spawning, data collection, and visualization during the simulation.

3.1 Dynamic Grass Spawning


csharp
Copy code
if (Random.Range(0f, 1f) < grass_spawn_rate) {
Instantiate(Grass, new Vector3(Random.Range(-50f, 50f), 1f,
Random.Range(-50f, 50f)), Quaternion.identity);
}

Explanation:

• New grass spawns with a probability defined by grass_spawn_rate.


• This ensures the ecosystem has a renewable resource for rabbits to consume.

3.2 Time Tracking

csharp
Copy code
time += Time.deltaTime; // Increment the timer.

if (time > 1) { // Check if 1 second has passed.


time = 0; // Reset the timer.
...
}

Purpose:

• Executes population tracking and logging every second.

3.3 Population Tracking

csharp
Copy code
GameObject[] foxes_objects = GameObject.FindGameObjectsWithTag("Fox");
GameObject[] rabbits_objects = GameObject.FindGameObjectsWithTag("Rabbit");

rabbitnumber = rabbits_objects.Length; // Count current rabbits.


foxnumber = foxes_objects.Length; // Count current foxes.

Explanation:

• Uses GameObject.FindGameObjectsWithTag() to count all foxes and rabbits in the


scene.

3.4 Calculate Average Speeds


csharp
Copy code
float fox_current_speed = 0;
foreach (GameObject fox in foxes_objects) {
fox_current_speed += fox.GetComponent<Fox>().speed; // Sum up fox speeds.
}
fox_current_speed /= foxes_objects.Length; // Calculate average speed.

float rabbit_current_speed = 0;
foreach (GameObject rabbit in rabbits_objects) {
rabbit_current_speed += rabbit.GetComponent<Rabbit>().speed; // Sum up
rabbit speeds.
}
rabbit_current_speed /= rabbits_objects.Length; // Calculate average speed.

3.5 Store Data

csharp
Copy code
foxes.Add(foxes_objects.Length); // Store fox count.
rabbits.Add(rabbits_objects.Length); // Store rabbit count.
fox_speed.Add(fox_current_speed); // Store average fox speed.
rabbit_speed.Add(rabbit_current_speed); // Store average rabbit speed.

3.6 Update Chart

csharp
Copy code
lineChart.AddData(0, Time.time, foxnumber); // Update fox population
data.
lineChart.AddData(1, Time.time, rabbitnumber); // Update rabbit population
data.

Explanation:

• Plots the populations of foxes and rabbits on a line chart for real-time visualization.

Key Concepts

1. Initialization: Spawning foxes, rabbits, and grass with randomized properties.


2. Dynamic Resource Management: Grass is added dynamically to simulate a renewable
resource.
3. Data Collection: Tracks population sizes and average speeds of animals over time.
4. Visualization: Displays population trends in a line chart for monitoring the simulation.

You might also like