# Minecraft - Water Physics

Hello!

I’m making a minecraft clone and I want to create some water too!
I made a script that checks whether an object exists next to the water object and if there’s no object there, it creates another water object which does the same thing. So it’s like a loop. The problem is that it doesn’t creates the water object from all of it sides when there’s no object there. What’s the problem?

Here’s the script:

``````var waterObj : Transform;
var hit : RaycastHit;
var distance : float = 1.0;

function Update() {
if(Physics.Raycast(transform.position,Vector3(transform.position.x+1,transform.position.y,transform.position.z),hit,distance)) {
return;
} else {
Instantiate(waterObj,Vector3(transform.position.x+1,transform.position.y,transform.position.z),Quaternion.identity);
}
if(Physics.Raycast(transform.position,Vector3(transform.position.x-1,transform.position.y,transform.position.z),hit,distance)) {
return;
} else {
Instantiate(waterObj,Vector3(transform.position.x-1,transform.position.y,transform.position.z),Quaternion.identity);
}
if(Physics.Raycast(transform.position,Vector3(transform.position.x,transform.position.y,transform.position.z+1),hit,distance)) {
return;
} else {
Instantiate(waterObj,Vector3(transform.position.x,transform.position.y,transform.position.z+1),Quaternion.identity);
}
if(Physics.Raycast(transform.position,Vector3(transform.position.x,transform.position.y,transform.position.z-1),hit,distance)) {
return;
} else {
Instantiate(waterObj,Vector3(transform.position.x,transform.position.y,transform.position.z-1),Quaternion.identity);
}
if(Physics.Raycast(transform.position,Vector3(transform.position.x,transform.position.y-1,transform.position.z),hit,distance)) {
return;
} else {
Instantiate(waterObj,Vector3(transform.position.x,transform.position.y-1,transform.position.z),Quaternion.identity);
}
}
``````

Perfect use of a recursive function:

** Note ** : I haven’t tested this, use this as an idea to recursively fill a location. Bear in mind that if the water boundaries are not properly set, or if there is a bug, this can get into an infinite loop !

``````function Update()
{
var waterObj : Transform;  // The prefab
var startWaterObj: Transform // The starting water block

MakeWater(startWaterObj);
}

function MakeWater(Transform t)
{
var hit : RaycastHit;
var distance : float = 1.0;
var clone: Transform;

if(Physics.Raycast(t.position,t.forward, distance) {
clone = Instantiate(waterObj, t.position + (Vector3.forward * distance), Quaternion.identity);
MakeWater(clone);
}

if(Physics.Raycast(t.position,t.back, distance) {
clone = Instantiate(waterObj, t.position + (Vector3.back * distance), Quaternion.identity);
MakeWater(clone);
}

if(Physics.Raycast(t.position,t.left, distance) {
clone = Instantiate(waterObj, t.position + (Vector3.left * distance), Quaternion.identity);
MakeWater(clone);
}

if(Physics.Raycast(t.position,t.right, distance) {
clone = Instantiate(waterObj, t.position + (Vector3.right * distance), Quaternion.identity);
MakeWater(clone);
}
}
``````