Unity Tutorial: Setting Up Idle and Run Animations

 7 min video

 2 min read

YouTube video ID: AdQz2wStdLY

Source: YouTube video by Wild Cockatiel GamesWatch original video

PDF

Start by organizing the project assets. In the Assets folder, create a new sub‑folder named animations. Inside this folder, add an Animator Controller called _player_anim. Next, generate two animation clips: one named idle and another named run. Open each clip in the Animation window and enable Loop Time so the animations repeat continuously. Finally, select the player GameObject, add an Animator component, and assign _player_anim as its controller.

Animator Configuration

Open the Animator window via Window > Animation > Animator. Drag the idle clip into the grid; it becomes the orange default state automatically. Drag the run clip beside it. In the Parameters tab, create a Boolean parameter named isRunning. Create a transition from idle to run and another from run back to idle. For both transitions, uncheck Has Exit Time to allow immediate switching. Set the transition conditions: isRunning = true for the idle‑to‑run transition and isRunning = false for the run‑to‑idle transition. Finally, set Transition Duration to 0 to eliminate any lag between states.

Scripting and Implementation

In the player movement script, declare a private field for the Animator and expose it in the Inspector:

[SerializeField] private Animator animator;

Within the movement logic, check whether the input vector is non‑zero. If input is detected, call:

animator.SetBool("isRunning", true);

If no input is present, call:

animator.SetBool("isRunning", false);

After saving the script, return to the Unity Editor and drag the player’s Animator component onto the script’s animator slot in the Inspector. This links the script to the Animator controller, enabling the Boolean parameter to drive the idle and run animations based on player input.

How the System Works

Sprite animation is created by dragging a sequence of sprite frames into the Animation window, which automatically generates keyframes. The speed of the animation can be adjusted by moving the keyframe handle. The Animator operates as a state machine; Boolean parameters trigger transitions between states. Disabling Has Exit Time lets the state machine interrupt the current animation instantly when the parameter changes. The C# script monitors keyboard or joystick input, setting isRunning to true when movement occurs and false when the player stops, causing the Animator to switch between idle and run states without delay.

  Takeaways

  • Create an "animations" folder, an Animator Controller named _player_anim, and two looping clips called idle and run, then attach the Animator component to the player object.
  • In the Animator window, set idle as the default state, add a Boolean parameter isRunning, and create transitions between idle and run with Has Exit Time disabled.
  • Set each transition’s condition to isRunning true for idle‑to‑run and false for run‑to‑idle, and set Transition Duration to 0 to eliminate input lag.
  • In the player movement script, expose a private Animator field, detect non‑zero input, and call animator.SetBool("isRunning", true) or false accordingly.
  • Drag the Animator component onto the script’s inspector slot, ensuring the parameter name matches exactly, which enables immediate, input‑driven animation switching.

Frequently Asked Questions

How does disabling "Has Exit Time" affect animation transitions in Unity?

Disabling Has Exit Time removes the built‑in wait for the current clip to finish before a transition can occur. This lets the Animator interrupt the current animation as soon as the Boolean condition changes, providing immediate response to player input and preventing lag between idle and run states.

What is the correct way to set up the isRunning parameter for instant state changes?

Create a Boolean named exactly "isRunning" in the Animator, set the transition conditions to isRunning = true for idle→run and isRunning = false for run→idle, disable Has Exit Time, and set Transition Duration to 0. Then, from script, call animator.SetBool("isRunning", true) when movement input is detected and false when it stops.

Who is Wild Cockatiel Games on YouTube?

Wild Cockatiel Games is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.

Does this page include the full transcript of the video?

Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.

How the System Works

Sprite animation is created by dragging a sequence of sprite frames into the Animation window, which automatically generates keyframes. The speed of the animation can be adjusted by moving the keyframe handle. The Animator operates as a state machine; Boolean parameters trigger transitions between states. Disabling **Has Exit Time** lets the state machine interrupt the current animation instantly when the parameter changes. The C# script monitors keyboard or joystick input, setting `isRunning` to true when movement occurs and false when the player stops, causing the Animator to switch between idle and run states without delay.

Helpful resources related to this video

If you want to practice or explore the concepts discussed in the video, these commonly used tools may help.

Links may be affiliate links. We only include resources that are genuinely relevant to the topic.

PDF