Search Unity

Animator - characters all run animation

Discussion in 'Animation' started by 0315, May 29, 2020.

  1. 0315

    0315

    Joined:
    Nov 19, 2015
    Posts:
    3
    Hi,
    I have a problem with animator and various characters.
    I have a couple of characters (prefabs from package).
    There are various animations for them.
    I prepared one Animator which runs walk-animation and if triggered runs dead-animation.
    I connected this Animator to various characters.
    I created one script that randomly sets target for a character and characters then moves towards this point.
    I created script that detects when a character is hit and if hit (onMouseClick) then dead-animation plays.

    The problem: when I click on one character the dead-animation plays for all other characters also (not only for this one).
    How can I achieve the goal, which is play only dead animation on only one character.

    script of the character:
    Code (CSharp):
    1. public class NPCScript : MonoBehaviour
    2. {
    3.  
    4.     public Animator anim;
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.         anim = GetComponent<Animator>();
    9.     }
    10.  
    11.  
    12.     void setDying()
    13.     {
    14.         currentState = 3;
    15.         anim.SetInteger(animatorMovingStateName, currentState);
    16.     }
    17.  
    18.     void Update()
    19.     {
    20.         if (Input.GetMouseButtonDown(0))
    21.         {
    22.             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    23.             RaycastHit hit;
    24.  
    25.             if (Physics.Raycast(ray, out hit))
    26.             {
    27.                 if (hit.transform.name == "NPC")
    28.                 {
    29.                     setDying();
    30.                 }
    31.             }
    32.         }
    33.     }
    34. }
     
  2. 0315

    0315

    Joined:
    Nov 19, 2015
    Posts:
    3
    problem solved. all NPC`s run the Update code - then all run setDying() animation. It should be only run by NPC that received the hit.