Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

C# - OnMouseDown vs OnMouseButtonDown - How can I rewrite this, make it better?

Discussion in 'Scripting' started by Trickzbunny, Nov 11, 2016.

  1. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    Hey!

    I have a Well and as a child a particle system of water splashing.
    If I click the well, it plays an animation (gets bigger then smaller) and then plays my particle system which is the water splashing.

    However, If i hold down the mouse the particlesystem continuously plays, and if i do OnMouseButtonDown it doesnt play at all.
    The thing is, you will be clicking the well very often, (spam clicking for water) now is it better to instantiate the particleFX for x Time then delete it after y Time? or what should I do, and preferably how?

    Code (CSharp):
    1. public class ClickAnimation : MonoBehaviour
    2. {
    3.     public GameObject MyAnimatorObject;
    4.     private Animator myAnimator;
    5.  
    6.     public bool waterPlay;
    7.     public ParticleSystem waterFountain;
    8.  
    9.  
    10.     void Start ()
    11.     {
    12.         myAnimator = MyAnimatorObject.GetComponent<Animator> ();
    13.     }
    14.        
    15.     void Update ()
    16.     {
    17.         if (waterPlay)
    18.         {
    19.             waterFountain.Play ();
    20.             waterFountain.GetComponent<ParticleSystem>().enableEmission = true;
    21.         }
    22.  
    23.         else if (!waterPlay)
    24.         {
    25.             waterFountain.Stop();
    26.         }
    27.     }
    28.        
    29.  
    30.     void OnMouseDown()
    31.     {
    32.         myAnimator.Play ("WellClicked");
    33.         waterPlay = true;
    34.     }
    35.  
    36.     void OnMouseUp()
    37.     {
    38.         waterPlay = false;
    39.     }
    40.  
    41. }
    42.  
     
  2. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    It's hard to say how to do it without properly defining what you want to happen.

    I would suggest ditching OnMouseXXX in favour of the EventSystem interfaces.
     
  3. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    Pretty much in easier to picture terms:

    A clicker game with a chest, each time u click on it, the chest shakes (plays the animation) and has few coins from the Particle system fall out, lets say 3-4 coins (predetermined in the settings of the particle system)

    In my case its just a fountain and water splashing out :)
    But each time it gets a click, I guess it would be nicer to finish the particlesystem going on (e.g. the 3-4 coins falling out) and let an extra 3-4 fall out.

    (PS: this is not like a normal clicker, so you dont click the "coins" or water to pick up later. hence the reason why i am using a particle system and not gameObjects.)
     
  4. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
  5. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    Thanks, didnt know about that, but running into an error since I am not sure how to implement it
    I added this which is causing the error:
    Code (CSharp):
    1.     public void Emit (int count);
    My Error: "must have a body because it is not marked abstract, extern, or partial."
    and changed the Update and made it into:
    Code (CSharp):
    1. void Update () {
    2.         if (waterPlay) {
    3.             waterFountain.Play ();
    4.             waterFountain.GetComponent<ParticleSystem>().Emit (5);
    5.         }
    6.         else if (!waterPlay){
    7.             waterFountain.Stop();
    8.         }
    9.     }
     
  6. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    Delete that line altogether.
     
  7. Trickzbunny

    Trickzbunny

    Joined:
    Jun 26, 2015
    Posts:
    64
    Yay, got it! :)
    Also had to get rid off the waterPlay bool and move the Emit(5) down to OnMouseDown and everything works :)

    Thanks a lot
     
    Last edited: Nov 11, 2016
    Kiwasi likes this.