Search Unity

Splash (Again)

Discussion in 'Scripting' started by MetroidHunter, Jul 22, 2007.

  1. MetroidHunter

    MetroidHunter

    Joined:
    May 22, 2007
    Posts:
    213
    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:



     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    I did this some time ago. Yoggy did some better stuff with water here, but it requires Pro.

    --Eric
     
  3. MetroidHunter

    MetroidHunter

    Joined:
    May 22, 2007
    Posts:
    213
    thanks... but i don't want to get all deep. is there a simpler (less expensive :roll: ) way?
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    My technique doesn't require Pro, so do something similar to that.

    --Eric
     
  5. MetroidHunter

    MetroidHunter

    Joined:
    May 22, 2007
    Posts:
    213
    Here is the script im trying:


    Code (csharp):
    1. /// Spawns an object (often a particle system).
    2.  
    3. // Anything declared with var outside a function is visible and can be changed in the inspector
    4. var splash : GameObject;
    5.  
    6. function OnCollisionEnter () {
    7.     // Instantiate the explosion object if we assigned one in the inspector!
    8.     if (explosion != null) {
    9.         instantiatedExplosion = Instantiate (explosion, transform.position, transform.rotation
    10.         }


    it doesn't work because it won't register the "}". Do you know what's wrong? :roll:
     
  6. AaronC

    AaronC

    Joined:
    Mar 6, 2006
    Posts:
    3,552
    I think it either needs to be explosion or splash-one refrences the other, so use either not both?
    Code (csharp):
    1. var explosion : GameObject;
    2.  
    3. function OnCollisionEnter () {
    4.    // Instantiate the explosion object if we assigned one in the inspector!
    5.    if (explosion != null) {
    6.       instantiatedExplosion = Instantiate (explosion, transform.position, transform.rotation
    7.       }
    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
    Code (csharp):
    1.  
    2. var splash : GameObject;
    3.  
    4. function OnCollisionEnter () {
    5.    // Instantiate the explosion object if we assigned one in the inspector!
    6.    if (splash != null) {
    7.      Instantiate (splash, transform.position, transform.rotation
    8.       }
    9. }
    Edit>>>>>>>>> I missed a bracket-bottom code should work now :oops:
     
  7. MetroidHunter

    MetroidHunter

    Joined:
    May 22, 2007
    Posts:
    213
    OH! here is someone's breakVase script i have modified (it doesn't have the collide with Tag, or work yet):


    Code (csharp):
    1. var brokenVase : Transform;
    2. var radius = 2.0;         // The radius of the explosion that makes the vase shatter instead of just falling apart
    3. var power = 10.0;         // The power of the explosion (later on this will be multiplied by the velocity,
    4.                      // so the vase shatters more if it falls farther)
    5. var fragility = 12;         // This is for the relative velocity when the vase hits...
    6.                      // bigger numbers for fragility make the vase harder to break
    7. private var numberOfCollisions = 1;   // A count for the number of objects a vase hits at once...
    8.                            // we only want 1 or else you might get multiple shattered vases at once
    9.  
    10. // We want relative velocity info; otherwise this could be written as "function OnCollisionEnter(collision) {"
    11. // and it would be a bit faster
    12. function OnCollisionEnter(collision : Collision) {
    13.  
    14.    // If the vase isn't going fast enough to break or a collision has already been processed, then don't do anything
    15.    if (collision.relativeVelocity.magnitude < fragility || numberOfCollisions != 1) {return;}
    16.    else  
    17.  
    18.       // Get the velocity from the vase and apply it to the pieces of the broken vase
    19.       // I figure the velocity would be reduced some by the impact if the vase was really shattering and not faked,
    20.       // so multiply by .6...looks more or less right anyway, leaving it at 1 doesn't really
    21.       var vaseVelocity = rigidbody.velocity;
    22.       for (var child in clonedVase) {
    23.          child.rigidbody.velocity = vaseVelocity*.6;
    24.       }
    25.        
    26.       // Do the explosion force thing on nearby objects--the vase shards--in an attempt to simulate a properly breaking vase
    27.       var explosionPos = transform.position;
    28.       var colliders : Collider[] = Physics.OverlapSphere (explosionPos, radius);
    29.              
    30.       for (var hit in colliders) {
    31.          if (!hit)
    32.              continue;
    33.              
    34.          if (hit.rigidbody) {
    35.             hit.rigidbody.AddExplosionForce(power*collision.relativeVelocity.magnitude, explosionPos, radius, 2.0);
    36.          }
    37.       }
    38.    }
     
  8. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    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

    Code (csharp):
    1.  
    2. /// Spawns an object (often a particle system).
    3.  
    4. // Anything declared with var outside a function is visible and can be changed in the inspector
    5. var splash : GameObject;
    6.  
    7. function OnCollisionEnter () {
    8.    // Instantiate the explosion object if we assigned one in the inspector!
    9.    if (explosion != null) {
    10.       instantiatedExplosion = Instantiate (explosion, transform.position, transform.rotation
    11.       }
    12.  
    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.

    Code (csharp):
    1.  
    2. // the splash prefab
    3. var splash : GameObject;
    4.  
    5.  
    6. // called whenever an object hits us
    7. function OnCollisionEnter (collision : Collision) {
    8.     // Instantiate the splash object if we assigned one in the inspector!
    9.     if (splash != null) {
    10.        
    11.         // this part gets the correct point for the explosion out of all the explosion contacts
    12.         var hitPoint : Vector3;
    13.         for(contact in collision.contacts)
    14.         {
    15.             hitPoint += contact.point;
    16.         }
    17.         hitPoint /= collision.contacts.length;
    18.        
    19.         // do the instantiate. Make sure to clean up these splashes once they are done, with
    20.         //TimedObjectDestroy or something
    21.         Instantiate (splash, hitPoint, transform.rotation);
    22.     }
    23. }
    24.  
     
  9. MetroidHunter

    MetroidHunter

    Joined:
    May 22, 2007
    Posts:
    213
    Yeah, it's a trigger. So do I... what DO I do? :roll:
     
  10. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    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.
     
  11. MetroidHunter

    MetroidHunter

    Joined:
    May 22, 2007
    Posts:
    213
    I'm Using your water script.
     
  12. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    What is falling into the water in this case? if it is small objects, this might work ok.

    Code (csharp):
    1.  
    2. // the splash prefab
    3. var splash : GameObject;
    4.  
    5. // called whenever an object hits us
    6. function OnTriggerEnter (other : Collider) {
    7.    // Instantiate the splash object if we assigned one in the inspector!
    8.    if (splash != null) {
    9.      
    10.       // do the instantiate. Make sure to clean up these splashes once they are done, with
    11.       //TimedObjectDestroy or something
    12.       Instantiate (splash, other.transform.position, transform.rotation);
    13.    }
    14. }
    15.