Splash (Again)

I cant make it so that the water creates a splash when hit by an object. i’m looking to instantiate a prefab when there is a collision var power. does anyone have suggestions? or scripts? :roll:

I did this some time ago. Yoggy did some better stuff with water here, but it requires Pro.

–Eric

thanks… but i don’t want to get all deep. is there a simpler (less expensive :roll: ) way?

My technique doesn’t require Pro, so do something similar to that.

–Eric

Here is the script im trying:

/// Spawns an object (often a particle system).

// Anything declared with var outside a function is visible and can be changed in the inspector
var splash : GameObject;

function OnCollisionEnter () {
	// Instantiate the explosion object if we assigned one in the inspector!
	if (explosion != null) {
		instantiatedExplosion = Instantiate (explosion, transform.position, transform.rotation
		}

it doesn’t work because it won’t register the “}”. Do you know what’s wrong? :roll:

I think it either needs to be explosion or splash-one refrences the other, so use either not both?

var explosion : GameObject; 

function OnCollisionEnter () { 
   // Instantiate the explosion object if we assigned one in the inspector! 
   if (explosion != null) { 
      instantiatedExplosion = Instantiate (explosion, transform.position, transform.rotation 
      }

You can
Substitute the word splash for explosion, but if you do do it throughout the script…

Do you know what I mean? I altered it slightly try it and see…(untested)

AC

var splash : GameObject; 

function OnCollisionEnter () { 
   // Instantiate the explosion object if we assigned one in the inspector! 
   if (splash != null) { 
     Instantiate (splash, transform.position, transform.rotation 
      }
}

Edit>>>>>>>>> I missed a bracket-bottom code should work now :sweat_smile:

OH! here is someone’s breakVase script i have modified (it doesn’t have the collide with Tag, or work yet):

var brokenVase : Transform; 
var radius = 2.0;         // The radius of the explosion that makes the vase shatter instead of just falling apart 
var power = 10.0;         // The power of the explosion (later on this will be multiplied by the velocity, 
                     // so the vase shatters more if it falls farther) 
var fragility = 12;         // This is for the relative velocity when the vase hits... 
                     // bigger numbers for fragility make the vase harder to break 
private var numberOfCollisions = 1;   // A count for the number of objects a vase hits at once... 
                           // we only want 1 or else you might get multiple shattered vases at once 

// We want relative velocity info; otherwise this could be written as "function OnCollisionEnter(collision) {" 
// and it would be a bit faster 
function OnCollisionEnter(collision : Collision) { 

   // If the vase isn't going fast enough to break or a collision has already been processed, then don't do anything 
   if (collision.relativeVelocity.magnitude < fragility || numberOfCollisions != 1) {return;} 
   else  

      // Get the velocity from the vase and apply it to the pieces of the broken vase 
      // I figure the velocity would be reduced some by the impact if the vase was really shattering and not faked, 
      // so multiply by .6...looks more or less right anyway, leaving it at 1 doesn't really 
      var vaseVelocity = rigidbody.velocity; 
      for (var child in clonedVase) { 
         child.rigidbody.velocity = vaseVelocity*.6; 
      } 
       
      // Do the explosion force thing on nearby objects--the vase shards--in an attempt to simulate a properly breaking vase 
      var explosionPos = transform.position; 
      var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
             
      for (var hit in colliders) { 
         if (!hit) 
             continue; 
              
         if (hit.rigidbody) { 
            hit.rigidbody.AddExplosionForce(power*collision.relativeVelocity.magnitude, explosionPos, radius, 2.0); 
         } 
      } 
   }

Here is the code that you were using. it has some problems.

1: you don’t need to assign the instantiate to a var if you don’t do anything with it aftarwards.

2: You are missing a ); on the instantiate line

3: You are missing a } on the bottom

4: The instantiate won’t create an object at the collison point. It will place it at the center of the water every time

/// Spawns an object (often a particle system).

// Anything declared with var outside a function is visible and can be changed in the inspector
var splash : GameObject;

function OnCollisionEnter () {
   // Instantiate the explosion object if we assigned one in the inspector!
   if (explosion != null) {
      instantiatedExplosion = Instantiate (explosion, transform.position, transform.rotation
      }

Here is code that fixes all the problems and should work.

Note: If your water collider is a trigger you need to use OnTriggerEnter instead, but you can’t just switch it, it needs to be set up differently with OnTriggerEnter.

// the splash prefab
var splash : GameObject;


// called whenever an object hits us
function OnCollisionEnter (collision : Collision) {
	// Instantiate the splash object if we assigned one in the inspector!
	if (splash != null) {
		
		// this part gets the correct point for the explosion out of all the explosion contacts
		var hitPoint : Vector3;
		for(contact in collision.contacts)
		{
			hitPoint += contact.point;
		}
		hitPoint /= collision.contacts.length;
		
		// do the instantiate. Make sure to clean up these splashes once they are done, with 
		//TimedObjectDestroy or something
		Instantiate (splash, hitPoint, transform.rotation);
	}
}

Yeah, it’s a trigger. So do I… what DO I do? :roll:

For now, you can make it not a trigger and give it a physics material with a spring. It is possible to achieve decent water this way I think. Using a trigger might be harder.

I’m Using your water script.

What is falling into the water in this case? if it is small objects, this might work ok.

// the splash prefab
var splash : GameObject;

// called whenever an object hits us
function OnTriggerEnter (other : Collider) {
   // Instantiate the splash object if we assigned one in the inspector!
   if (splash != null) {
      
      // do the instantiate. Make sure to clean up these splashes once they are done, with
      //TimedObjectDestroy or something
      Instantiate (splash, other.transform.position, transform.rotation);
   }
}