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

Problem with Actions

Discussion in '2D' started by Oddinx, Dec 23, 2019.

  1. Oddinx

    Oddinx

    Joined:
    Mar 2, 2017
    Posts:
    12
    Hi there.

    I'm making a platformer game, in the game my character gets powerups when collides with certain objects.what I want is the enemies stop their movemen and animations when the player touch the powerups, and move again after my character obtain the powerup.

    But the problem is that when the actions are applied to the enemies,the enemies keeps stopped.

    This is the code of the manager

    Code (CSharp):
    1. public event Action pausa;
    2.  
    3.     public event Action reanudar;
    4.  
    5.     public event Action suscribir;
    6.  
    7.     public event Action desuscribir;
    8.  
    9.     public void Suscripcion(){
    10.  
    11.       if( suscribir!= null){
    12.  
    13.           suscribir();
    14.       }
    15.  
    16.     }
    17.  
    18.        public void Desuscripcion(){
    19.  
    20.       if( desuscribir!= null){
    21.  
    22.           desuscribir();
    23.       }
    24.  
    25.     }
    26.    
    27.       public void Pausarenemigo(){
    28.  
    29.  
    30.    
    31.  
    32.           if(pausa !=null){
    33.  
    34.                pausa();
    35.       }
    36.  
    37.       }
    38.  
    39.  
    40.  
    41.       public void Reanudarenemigo(){
    42.  
    43.  
    44.  
    45.          // Pausaenemigo.detener = false;
    46.          if(reanudar!= null){
    47.  
    48.              reanudar();
    49.          }
    50.       }
    51.    
    52.  
    In this code I suscribe the actions to the enemies.

    Code (CSharp):
    1.   void Start()
    2.     {
    3.  
    4.  
    5.         Init();
    6.          Manager._manager.pausa+= OnPauseGame;
    7.  
    8.        Manager._manager.reanudar+=OnResumeGame;
    9.  
    10.        Manager._manager.suscribir += Suscribir;
    11.  
    12.        Manager._manager.desuscribir+= Desuscribir;
    13.  
    14.     }
    15.  
    16.  
    17.  
    18.  
    19.  
    20.     public void Suscribir(){
    21.  
    22.      
    23.           Manager._manager.pausa+= OnPauseGame;
    24.  
    25.        Manager._manager.reanudar+=OnResumeGame;
    26.  
    27.  
    28.    
    29.        
    30.  
    31.       }
    32.  
    33.     public void OnPauseGame(){
    34.  
    35.        
    36.  
    37.      
    38.  
    39.        controller.enabled = false;
    40.    
    41.         anim.enabled = false;
    42.    
    43.       //This part disables the movement script of the enemie
    44.         velocidadenemigo.enabled = false;
    45.        
    46.        Manager._manager.pausa-= OnPauseGame;
    47.  
    48.    
    49.  
    50.        
    51.        
    52.  
    53.     }
    54.  
    55.      public void Desuscribir(){
    56.  
    57.        
    58.         Manager._manager.pausa-=OnPauseGame;
    59.  
    60.           Manager._manager.reanudar-=OnResumeGame;
    61.  
    62.           Manager._manager.suscribir -= Suscribir;
    63.  
    64.            Manager._manager.desuscribir-= Desuscribir;
    65.  
    66.      
    67.  
    68.       }
    69.  
    70.         public void OnResumeGame() {
    71.  
    72.    
    73.        
    74.         velocidadenemigo.enabled = true;
    75.    
    76.         controller.enabled = true;
    77.  
    78.         anim.enabled = true;
    79.  
    80.          Manager._manager.reanudar-=OnResumeGame;
    81.      
    82.    
    83.        
    84.      
    85.      }
    in this code the actions are triggered

    Code (CSharp):
    1.   void Update()
    2.     {
    3.    
    4.          
    5.        
    6.        switch (estado){
    7.            
    8.  
    9.          case 0:
    10.    
    11.  
    12.          //animator.runtimeAnimatorController = Marios[0];
    13.  
    14.          anim.SetLayerWeight (1, 0);
    15.  
    16.    
    17.  
    18.          break;
    19.  
    20.          case 1:
    21.  
    22.          
    23.  
    24.          Crecer();
    25.          
    26.  
    27.          break;
    28.      
    29.  
    30.          case 2:
    31.  
    32.  
    33.  
    34.          break;
    35.  
    36.        }
    37.      
    38.  
    39.  
    40.     }
    41.  
    42.  
    43.  
    44. void Crecer(){
    45.   Manager._manager.Suscripcion();
    46. anim.SetBool ("Crecer",true);
    47. transtimer += Time.fixedDeltaTime;
    48.  
    49.  
    50.  
    51. Manager._manager.Pausarenemigo();
    52.  
    53.      
    54.       if(transtimer > 1f){
    55.  
    56.          anim.SetBool("Crecer",false);
    57.  
    58.          Manager._manager.Reanudarenemigo();
    59.  
    60.  
    61.        
    62.  
    63.        
    64.  
    65.        
    66.      }
    67.  
    68.      anim.SetLayerWeight (1, 1);
    69.  
    70.  
    71.  
    72.  
    73. }
    74.  
    75.  
     
  2. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    there no anymore problems with actions :)

    tuts
    Learn to Program by Making Games in Unity 2017


    I thinking what in project Alien Hunters you find all your answers


    happy holidays ;)
     
  3. ZliVuk

    ZliVuk

    Joined:
    Mar 24, 2018
    Posts:
    61
    I stop my monsters by
    - disabling Animator: animator.enabled = false;
    - making it static: gameObject.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Static;
    Moving it again:
    - enable Animator: animator.enabled = true;
    - dynamic: gameObject.GetComponent<Rigidbody2D>().bodyType = RigidbodyType2D.Dynamic;
    In Update() check if gameObject.GetComponent<Rigidbody2D>().bodyType == RigidbodyType2D.Dynamic and apply velocity.