Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by sander110419 · Mar 12, 2016 at 02:39 PM · button trigger events

Figuring out which function to call on button click

So I am an utter noob, but I spent the last 4 hours looking for a solution myself, but alas, I can't find what I need.

I'm using a prefab called LiquidPhysics.

I have 2 scripts, and I need a button to toggle between types of liquid:

ParticleGenerator.cs

 using UnityEngine;
 using System.Collections;
 /// <summary
 /// Particle generator.
 /// 
 /// The particle generator simply spawns particles with custom values. 
 /// See the Dynamic particle script to know how each particle works..
 /// 
 /// Visit: www.codeartist.mx for more stuff. Thanks for checking out this example.
 /// Credit: Rodrigo Fernandez Diaz
 /// Contact: q_layer@hotmail.com
 /// </summary>
 
 public class ParticleGenerator : MonoBehaviour {        
     float SPAWN_INTERVAL=0.025f; // How much time until the next particle spawns
     float lastSpawnTime=float.MinValue; //The last spawn time
     public int PARTICLE_LIFETIME=3000000; //How much time will each particle live
     public Vector3 particleForce; //Is there a initial force particles should have?
     public DynamicParticle.STATES particlesState=DynamicParticle.STATES.WATER; // The state of the particles spawned
     public Transform particlesParent; // Where will the spawned particles will be parented (To avoid covering the whole inspector with them)
 
     void Start() {     }
 
     void Update() {    
         if( lastSpawnTime+SPAWN_INTERVAL<Time.time ){ // Is it time already for spawning a new particle?
             GameObject newLiquidParticle=(GameObject)Instantiate(Resources.Load("LiquidPhysics/DynamicParticle")); //Spawn a particle
             newLiquidParticle.GetComponent<Rigidbody2D>().AddForce( particleForce); //Add our custom force
             DynamicParticle particleScript=newLiquidParticle.GetComponent<DynamicParticle>(); // Get the particle script
             particleScript.SetLifeTime(PARTICLE_LIFETIME); //Set each particle lifetime
             particleScript.SetState(particlesState); //Set the particle State
             newLiquidParticle.transform.position=transform.position;// Relocate to the spawner position
             newLiquidParticle.transform.parent=particlesParent;// Add the particle to the parent container            
             lastSpawnTime=Time.time; // Register the last spawnTime            
         }        
     }
 }


AND

DynamicParticle.cs

 using UnityEngine;
 using System.Collections;
 /// <summary>
 /// Dynamic particle.
 /// 
 /// The dynamic particle is the backbone of the liquids effect. Its a circle with physics with 3 states, each state change its physic properties and its sprite color ( so the shader can separate wich particle is it to draw)
 /// The particles scale down and die, and have a scale  effect towards their velocity.
 /// 
 /// Visit: www.codeartist.mx for more stuff. Thanks for checking out this example.
 /// Credit: Rodrigo Fernandez Diaz
 /// Contact: q_layer@hotmail.com
 /// </summary>
 
 public class DynamicParticle : MonoBehaviour {    
     public enum STATES{WATER,GAS,LAVA,POO,NONE}; //The 4 states of the particle
     STATES currentState=STATES.NONE; //Defines the currentstate of the particle, default is water
     public GameObject currentImage; //The image is for the metaball shader for the effect, it is onle seen by the liquids camera.
     public GameObject[] particleImages; //We need multiple particle images to reduce drawcalls
     float GAS_FLOATABILITY=7.0f; //How fast does the gas goes up?
     float particleLifeTime=3.0f,startTime;//How much time before the particle scalesdown and dies    
 
     void Awake(){ 
         if (currentState == STATES.NONE)
             SetState (STATES.WATER);
     }
 
     //The definitios to each state
     public void SetState(STATES newState){
         if(newState!=currentState){ //Only change to a different state
             switch(newState){
                 case STATES.WATER:                                                    
                     GetComponent<Rigidbody2D>().gravityScale=1.0f; // To simulate Water density
                 break;
                 case STATES.GAS:        
                     particleLifeTime=particleLifeTime/2.0f;    // Gas lives the time the other particles
                     GetComponent<Rigidbody2D>().gravityScale=0.0f;// To simulate Gas density
                     gameObject.layer=LayerMask.NameToLayer("Gas");// To have a different collision layer than the other particles (so gas doesnt rises up the lava but still collides with the wolrd)
                 break;                    
                 case STATES.LAVA:
                     GetComponent<Rigidbody2D>().gravityScale=0.3f; // To simulate the lava density
                 break;    
                    case STATES.POO:
                 GetComponent<Rigidbody2D>().gravityScale=4.1f; // To simulate the lava density
                 break;
                 case STATES.NONE:
                     Destroy(gameObject);
                 break;
             }
             if(newState!=STATES.NONE){
                 currentState=newState;
                 startTime=Time.time;//Reset the life of the particle on a state change
                 GetComponent<Rigidbody2D>().velocity=new Vector2();    // Reset the particle velocity    
                 currentImage.SetActive(false);
                 currentImage=particleImages[(int)currentState];
                 currentImage.SetActive(true);
             }
         }        
     }
     void Update () {
         switch(currentState){
             case STATES.WATER: //Water and lava got the same behaviour
                 MovementAnimation(); 
                 ScaleDown();
             break;
             case STATES.LAVA:
                 MovementAnimation();
                 ScaleDown();
             break;
         case STATES.POO:
             MovementAnimation();
             ScaleDown();
             break;
             case STATES.GAS:
                 if(GetComponent<Rigidbody2D>().velocity.y<50){ //Limits the speed in Y to avoid reaching mach 7 in speed
                     GetComponent<Rigidbody2D>().AddForce (new Vector2(0,GAS_FLOATABILITY)); // Gas always goes upwards
                 }
                 ScaleDown();
             break;
 
         }    
     }
     // This scales the particle image acording to its velocity, so it looks like its deformable... but its not ;)
     void MovementAnimation(){
         Vector3 movementScale=new Vector3(1.0f,1.0f,1.0f);//Tamaño de textura no de metaball            
         movementScale.x+=Mathf.Abs(GetComponent<Rigidbody2D>().velocity.x)/30.0f;
         movementScale.z+=Mathf.Abs(GetComponent<Rigidbody2D>().velocity.y)/30.0f;
         movementScale.y=1.0f;        
         currentImage.gameObject.transform.localScale=movementScale;
     }
     // The effect for the particle to seem to fade away
     void ScaleDown(){ 
         float scaleValue = 1.0f-((Time.time-startTime)/particleLifeTime);
         Vector2 particleScale=Vector2.one;
         if (scaleValue <= 0) {
                         Destroy (gameObject);
         } else{
             particleScale.x=scaleValue;
             particleScale.y=scaleValue;
             transform.localScale=particleScale;
         }
     }
 
     // To change particles lifetime externally (like the particle generator)
     public void SetLifeTime(float time){
         particleLifeTime=time;    
     }
     // Here we handle the collision events with another particles, in this example water+lava= water-> gas
     void OnCollisionEnter2D(Collision2D other){
         if(currentState==STATES.WATER && other.gameObject.tag=="DynamicParticle"){ 
             if(other.collider.GetComponent<DynamicParticle>().currentState==STATES.LAVA){
                 SetState(STATES.GAS);
             }
         }
 
     }
     
 }
 

And I have no Idea how and which function I should add where in my button component. Should I write my own function that forwards a specific script to set the particle state to DynamicParticle.STATES.LAVA for example?

There is just too much going on for my brain to understand at this time...

Any help is greatly appreciated!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How can i change a state when a button is pressed? 1 Answer

Dragging a C# script to onClick button returns no listed functions 0 Answers

UI buttons take too much to time to unpress on mobile 0 Answers

Im trying to make gameObject move only if specific button is pressed 1 Answer

How to wait for a button response 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges