Create a 2D fluid demo in Unity using C#, in the simplest way possible
- User
Create a 2D fluid demo in Unity using C#, in the simplest way possible
- Assistant
Create a 2D fluid demo in Unity using C#, in the simplest way possible
Creating a 2D fluid simulation in Unity can be an interesting project. We'll focus on creating a simple "smoke" effect using a grid-based approach, which is computationally less intensive than full fluid dynamics but can still yield pleasing results. Here's a very basic implementation that achieves this effect using Unity's features and C# scripting.
Setup Unity Project
Empty GameObject in the scene named FluidSimulator.Sprite or Tilemap to visualize the fluid simulation. A simple grid of sprites can represent fluid density.Create the Fluid Simulation Script
Create a new C# script named FluidSimulation and attach it to the FluidSimulator GameObject.
using UnityEngine;
public class FluidSimulation : MonoBehaviour
{
private const int GridSize = 100;
private const float DiffusionRate = 0.1f;
private const float Viscosity = 0.1f;
private const float TimeStep = 0.1f;
private float[,] density;
private float[,] densityPrev;
private float[,] velX;
private float[,] velY;
private float[,] velPrevX;
private float[,] velPrevY;
void Start()
{
density = new float[GridSize, GridSize];
densityPrev = new float[GridSize, GridSize];
velX = new float[GridSize, GridSize];
velY = new float[GridSize, GridSize];
velPrevX = new float[GridSize, GridSize];
velPrevY = new float[GridSize, GridSize];
}
void Update()
{
// Add fluid or sources of density and velocity here. Example:
if (Input.GetMouseButton(0))
{
int x = (int)(Input.mousePosition.x / Screen.width * GridSize);
int y = (int)(Input.mousePosition.y / Screen.height * GridSize);
density[x, y] = 100;
velX[x, y] = 1;
velY[x, y] = 1;
}
SimulateFluid();
RenderDensity();
}
void SimulateFluid()
{
// Implement fluid simulation steps: Velocity step, Density step
// This is where you would invoke solver methods for diffusion, advection, and projection...
// Swap buffers
Swap(ref density, ref densityPrev);
Swap(ref velX, ref velPrevX);
Swap(ref velY, ref velPrevY);
}
void RenderDensity()
{
for (int x = 0; x < GridSize; x++)
{
for (int y = 0; y < GridSize; y++)
{
float d = density[x, y];
// Visualize density as colors (e.g., smoke intensity)
Debug.DrawLine(new Vector3(x, y), new Vector3(x, y) + Vector3.one * 0.1f * d, new Color(d, d, d));
}
}
}
private void Swap(ref float[,] a, ref float[,] b)
{
float[,] temp = a;
a = b;
b = temp;
}
}
Visualizing the Simulation
RenderDensity method uses Debug.DrawLine for visualizing the fluid. You can replace this with more advanced rendering by using sprites or shaders.SpriteRenderer to depict density levels in real-time, updating their colors based on the density array.Running and Testing
DiffusionRate, Viscosity, and TimeStep.This example is highly simplified and not physically accurate. For more realistic simulations:
This starter framework can be expanded upon depending on what specific fluid effects you want to simulate in Unity.
Not yet included in a published release.