Search Unity

Start animation every time I click/touch.

Discussion in '2D' started by Patta96, Oct 16, 2019.

  1. Patta96

    Patta96

    Joined:
    Nov 17, 2017
    Posts:
    4
    Hey lovely community,

    I want to create a simple clicker game for android and I want that a text appears every time I touch the screen. And actually it works fine but the problem is that I have to wait for my text animation to end. So if I touch the screen the text appears for 1 sec but if I press again within this sec nothing happens until the animation is over. So how can I make that my animation is played every time I touch the screen? I made a simple text animation with the animation tool where the text appears, moves up and disappears after one sec. I have honestly no idea how i can realize that. Some help would be awesome.

    Code:

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using UnityEngine.UI;
    public class TouchInput : MonoBehaviour
    {
    public GameObject animText;
    public static int eggCounter;
    public int internalEggCounter;
    void Update()
    {
    if (Input.touchCount == 1)
    {
    if (Input.GetTouch(0).phase == TouchPhase.Began)
    {
    Counter.counter += 1;
    internalEggCounter = eggCounter;
    animText.GetComponent<Text>().text = internalEggCounter + " Eggs";
    animText.GetComponent<Animation>().Play("animText");
    }
    }
    }
    }
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    First lesson is to use code blocks like this. Above your text box you will see the code insertion:
    upload_2019-10-16_14-57-17.png
    Click that and it will open up a panel you can paste your code into.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. public class TouchInput : MonoBehaviour
    6. {
    7. public GameObject animText;
    8. public static int eggCounter;
    9. public int internalEggCounter;
    10. void Update()
    11. {
    12. if (Input.touchCount == 1)
    13. {
    14. [code=CSharp]if (Input.GetTouch(0).phase == TouchPhase.Began)
    15. {
    16. Counter.counter += 1;
    17. internalEggCounter = eggCounter;
    18. animText.GetComponent<Text>().text = internalEggCounter + " Eggs";
    19. animText.GetComponent<Animation>().Play("animText");
    20.  
    21. Counter.counter = 0; // ADD THIS LINE OF CODE TO RESET THE VARIABLE
    22. }
    }
    }
    }[/code]

    This makes it much easier to read for us.

    Now to answer your question, you're forcing the animation to play only when touchCount equals 1. So You need to reset your variable after the animation is done playing.

    Code (CSharp):
    1. if (Input.GetTouch(0).phase == TouchPhase.Began)
    2. {
    3. Counter.counter += 1;
    4. internalEggCounter = eggCounter;
    5. animText.GetComponent<Text>().text = internalEggCounter + " Eggs";
    6. animText.GetComponent<Animation>().Play("animText");
    7.  
    8. // ADD THIS BELOW TO RESET YOUR COUNTER VARIABLE
    9. Counter.counter = 0;
    10. }
     

    Attached Files: