Search Unity

Resolved How to stop animation after hit projectile

Discussion in 'Animation' started by Frostysh, Aug 1, 2022.

  1. Frostysh

    Frostysh

    Joined:
    Jul 11, 2022
    Posts:
    66
    I have monster script program and projectile script program correspondly, the projectile has been throw by character.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class EnemyController : MonoBehaviour
    6. {
    7.     bool broken = true;
    8.     public float speed;
    9.     public float changeTime = 3.0f;
    10.     public bool vertical;
    11.    
    12.     Rigidbody2D rigidbody2D;
    13.     float timer;
    14.     int direction = 1;
    15.    
    16.     Animator animator;
    17.    
    18.     // Start is called before the first frame update.
    19.     void Start()
    20.     {
    21.         rigidbody2D = GetComponent<Rigidbody2D>();
    22.         timer = changeTime;
    23.         animator = GetComponent<Animator>();
    24.     }
    25.    
    26.     void Update()
    27.     {
    28.         if(!broken)
    29.         {
    30.             //Remember that "!" inverse the test, so if broken is true, !broken will be false and return won't be executed.
    31.             return;
    32.             //Exit function Void Update() early.
    33.         }
    34.  
    35.         timer -= Time.deltaTime;
    36.  
    37.         if (timer < 0)
    38.         {
    39.             direction = -direction;
    40.             timer = changeTime;
    41.         }
    42.     }
    43.    
    44.     void FixedUpdate()
    45.     {
    46.         if(!broken)
    47.         {
    48.             //Remember that "!" inverse the test, so if broken is true, !broken will be false and return won't be executed.
    49.             return;
    50.             //Exit function Void Update() early.
    51.         }
    52.  
    53.         Vector2 position = rigidbody2D.position;
    54.        
    55.         if (vertical)
    56.         {
    57.             position.y = position.y + Time.deltaTime*speed*direction;
    58.             animator.SetFloat("Abscissa", 0);
    59.             animator.SetFloat("Ordinate", direction);
    60.         }
    61.         else
    62.         {
    63.             position.x = position.x + Time.deltaTime*speed*direction;
    64.             animator.SetFloat("Abscissa", direction);
    65.             animator.SetFloat("Ordinate", 0);
    66.         }
    67.        
    68.         rigidbody2D.MovePosition(position);
    69.     }
    70.  
    71.     void OnCollisionEnter2D(Collision2D other)
    72.     {
    73.         Idiotic_Ruby_Controller player = other.gameObject.GetComponent<Idiotic_Ruby_Controller>();
    74.  
    75.         //Debug.Log(other.name);
    76.        
    77.         if (player != null)
    78.         {
    79.             player.ChangeHealth(-1);
    80.         }
    81.     }
    82.    
    83.     //Public, because we want it to be called from somewhere else, like Projectile script.
    84.     public void Disable()
    85.     {
    86.         broken = false;
    87.         rigidbody2D.simulated = false;
    88.     }
    89. }
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Projectile : MonoBehaviour
    6. {
    7.     Rigidbody2D rigidbody2d;
    8.    
    9.     // Start is called before the first frame update.
    10.     void Awake()
    11.     {
    12.         rigidbody2d = GetComponent<Rigidbody2D>();
    13.     }
    14.  
    15.     public void Launch(Vector2 direction, float force)
    16.     {
    17.         rigidbody2d.AddForce(force*direction);
    18.     }
    19.  
    20.     void OnCollisionEnter2D(Collision2D other)
    21.     {
    22.         //We also add a debug log to see what the projectile hit.
    23.         EnemyController e = other.collider.GetComponent<EnemyController>();
    24.         if (e != null)
    25.         {
    26.             e.Disable();
    27.         }
    28.  
    29.         Debug.Log("Projectile collision with " + other.gameObject);
    30.  
    31.         Destroy(gameObject);
    32.     }
    33.    
    34.     // Update is called once per frame.
    35.     void Update()
    36.     {
    37.        
    38.     }
    39. }
    The program public void Disable() in the EnemyController script should stop animation by disabling Rigidbody2D component, but somehow it don't want to... And despite a hit that we can see in the log, enemy robot soldier still moving like dancing "moob walk", walking on the same spot.

     
  2. Frostysh

    Frostysh

    Joined:
    Jul 11, 2022
    Posts:
    66
    Oops, there is solution of the problem in the next tutorial which I have not saw... It is made trough program
    animator.SetTrigger("Disabled")
    those resolved!