Animator help

Hey guys, I’m sorry for my horrible English .

I am new to Unity . I’m trying to make a 2D game , the character has a position when static and another when he is walking , I made them with two different sprites, stored in different prefabs and each has its animation.

Is there any way to make the transition one prefab to other prefab pressing the D for example?


Buenos dias!

Soy nuevo en Unity. Estoy tratando de hacer un juego en 2d, el personaje tiene una posicion cuando esta estático y otra cuando el esta caminando , yo los hice con dos sprites diferentes , almacenada en diferentes prefabs y cada una tiene su animación .

Hay alguna manera de hacer la transición de un prefab a otro al apretar la D por ejemplo?

A few things:

  1. Prefabs are GameObjects stored as assets, you usually “instantiate” a prefab into the scene. This creates a new object in the hierarchy that’s a clone of the prefab. You don’t want to use prefabs for the states of animations, you shouldn’t be using different player prefabs just to create a different animation.
  2. The animator moves between “states”, and each state has an animation clip. This is what you want, different animation clips, one for the “standing” animation and another for the “walking” animation.
  3. Your animation clip should switch between the sprites, and should be marked to “loop” (when you choose an animation clip you can set it to loop in the inspector).
  4. The animator uses transitions to move from a state to another. The transitions have conditions, and when those conditions happen, the whole transition happens. This means you need transitions with proper conditions to go from standing to walking AND from walking to standing. In your image you have only one transition, your player will never go back to standing that way.
  5. The conditions can be a lot, but you need to check at least 2 things. First, the “exit time” of the transitions shouldn’t be used to make sure the transitions happen instantly. Second, you need a custom parameter to tell the animator if the key “D” is being pressed. The easiest way to do it is adding a bool parameter in the animator (let’s call it “DKeyPressed”), set the standing->walking transition to happen when that parameter is true and set the walking->standing transition to happen when the parameter is false.
  6. To change the value of a parameter of the animator you have to get the animator component and call the proper “SetXXXX” function. In your case you want to set the DKeyPressed parameter to true when the D is pressed and false when isn’t.

So…

void Update() {
    GetComponent<Animator>().SetBool("DKeyPressed",Input.GetKey(KeyCode.D));
}