I am new to Unity and am using the most recent version. I want to make my player object play a 2D jump animation when I press the space bar. I already created slices from a 2D sprite sheet (in Assets/Character/Sprites). When I right click → create → animation → animation clip in Assets/Character/Animations, it will create the icon for an animation clip; but I don’t understand how to link this to the actual animation or sliced sprite sheet that I created. Other tutorials mention dragging the sliced sprite sheet onto the scene, but I don’t want to create a new GameObject in the world. I want my player to display the jumping animation when I press the space bar.
The animator also has state that is linking the motion field to the name of the animation clip (in Assets/Character/Animations).
I am doing this from the 2D scroller template. I see that they also have sprite sheets that are named after the jump animations, but when I name my sliced sprite sheet after my own animations or states, they do nothing. I am a bit confused. The documentation is making references to the fact that these things happen, but doesn’t say how to do it. Unity - Manual: Create a new Animation Clip , Unity - Manual: Animation States
Not looking to do this in code, since this really should be possible from the editor.
How do I link the animations in Assets/Character/Animations to the sprite sheet I have in Assets/Character/Sprites ??
import sprite sheet, ensure it is sliced correctly
As you have done, create new animation clip, setting up the frames (drag the sprites into the animation clip in this example we shall call the clip Jump) for each sprite you have in the sprite sheet, give it a frame in Jump (standard 2D animation usually works on a 24 frames per second, so if you want to make the sprite “Jump” in under a second divide your 24 by this value (12 frames for half a second etc)
Create an animation tree, this opens up as a Node based Graph, you do this by creating in your project files an animator controller
there is a node in there called ANY (this is good for switching animation states that are not linked or making animations you want to run concurrent.
drag in your animation clip into this State Graph, connecting it to the ANY state via a transition (setting transition rules) in your instance - Parameters - trigger, name your trigger
In your script you can set it to play the animation via a trigger (we can call that trigger Jump)
Here is example script for you
public Animator animator;
private void Awake(){
animator = GetComponent<Animator>();
}
public void OnJumpInput(InputAction.CallbackContext context){
if(context.phase == InputAction.Performed){
animator.SetTrigger("Jump");
}
The above code uses the Input system for unity, another way could be
public Animator animator;
private void Awake(){
animator = GetComponent<Animator>();
}
public void OnJumpInput(){
if(Input.GetKeyDown(KeyCode.Space){
animator.SetTrigger("Jump");
}