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. Join us on March 30, 2023, between 5 am & 1 pm EST, in the Performance Profiling Dev Blitz Day 2023 - Q&A forum and Discord where you can connect with our teams behind the Memory and CPU Profilers.
    Dismiss Notice

Question Active Mode and Passive Mode

Discussion in 'Animation' started by Palmiii, Jan 7, 2021.

  1. Palmiii


    Dec 22, 2020
    Hello everyone i hope someone can help me with my question.

    I want to implement in my 2D game an active mode and an passive mode. The active mode starts when you press left mouse button and ends after 20 seconds. After the 20 seconds when the button is not pressed again you return in passive mode.

    I tried with an Bool called IsAttacking but it only plays the animation once and the animation ends immediately.

    Code (CSharp):
    1.         {
    2.             if (Input.GetKey(KeyCode.Mouse0))
    3.             {
    4.                 anim.Play("Player_Hostile_Attack");
    5.             }
    6.         }
    8.         anim.SetBool("IsAttacking", IsAttacking());
    10.     public bool IsAttacking()
    11.     {
    12.         if (Input.GetKey(KeyCode.Mouse0))
    13.         {
    14.             return true;
    15.         }
    17.         return false;
    18.     }
    This is my code and i want the animation to play once and doesnt end before the animation played. I also want the public bool IsAttacking to be activated for 20 seconds after pressing Left Mouse Button.

    I hope someone can help me

    Attached Files: