Animating in Unity

Clint Wagoner
5 min readMar 7, 2023

--

Making our ship turn using Animations.

Setting Up the Animations

In this tutorial, we will go over animating our ship in Unity. The first thing you want to do is go to the ‘Window’ tab and open the ‘Animation’ window. Select the player and click on ‘Create’ in the animation window. Save your animation as ‘Idle’.

Since the ship will be facing this way when idle we don’t need to do anything to this specific animation. However, we will need to make two new animation clips. One for turning left and one for turning right.

Make a create a new clip and call it ‘TurnLeft’.

Next, click on the ‘AddProperty’ button and select Transform > Rotation.

We want the ship to rotate on the ‘Z’ axis so set the ‘Rotation.z’ property to -30 and delete the ending keyframes. You should notice the ship will rotate to the left.

Now repeat the process for the right turn. Create a new clip called ‘RightTurn’, Click ‘AddProperty’, set the ‘Rotation.z’ to -30, and delete the ending keyframes.

Setting Up Anim Parameters

Now we need to open the Animator window by selecting the ‘Window’ tab and opening the Animator window. After it opens, select the player game object in the hierarchy to set parameters for the player.

Select the parameters tab and hit the ‘+’ button to create new parameters. Let’s make three to correspond with each of our clips. Set each to ‘bool’ and name them ‘Idle’, ‘MovingLeft’, and ‘MovingRight’.

One of the things I like to do is set up my clips in the animator window to make sense to flow.

Now that the clips make sense, let’s add some transitions. Right-click the Idle animation and select ‘Make Transition’ and attach it to the ‘TurnLeft’ clip. Then, make another transition that goes back to the ‘Idle’ clip. Repeat this for the ‘TurnRight’ clip as well.

Now we just need to set the conditions of each transition and adjust the timing. Select the transition going to the ‘TurnLeft’ clip. Then in the inspector, under ‘Conditions’ click the ‘+’ button and set it to ‘MovingLeft’ and ‘true’. Then open the settings and change the ‘Exit Time’ to ‘0’.

Now select the transition going back to the Idle clip, add a new condition called ‘Idle’ and set it to ‘true’, then change the ‘Exit Time’ to ‘0’. Repeat the process for the ‘TurnRight’ clip.

You can tweak the transition settings to your liking. You can even turn off Has Exit Time altogether for faster animation.

Scripting the Animations

Now let’s set up our Player script to call these animations when we input left or right. We will need a variable for the Animator. Let’s create a new Animator type variable and call it ‘_anim’.

Now in FixedUpdate(), We use this for physics-based movement as opposed to plain old Update(), we need to call these animations. But we will create a new method called ‘HandleAnimation()’ and put our code there to keep things nice and tidy.

To unpack what is going on in the code, we are looking at the input from the player and using the SetBool() method to change the animation clip to the correct clip. And there you go! You now know how to create animations in Unity!

Thanks for reading!

Player script (Note: spawner objects will return null if you do not have them in your scene):

using UnityEngine;

public class Player : MonoBehaviour
{
[Header("Player Controls")]
[SerializeField] private float _moveForce = 5;
[SerializeField] private float _xBounds = 12f;
[SerializeField] private float _yBounds = 5;

[Header("Player Health")]
[SerializeField] private int _startingHealth = 1;
[SerializeField] private int _lives = 3;

private int _health = 1;
private Rigidbody _rigidbody;
private Animator _anim;
private SpawnManager _spawnManagerAsteroid;
private SpawnManager _spawnManagerEnemy;

private float _inputX;
private float _inputY;

void Start()
{
_rigidbody= GetComponent<Rigidbody>();
_anim = GetComponent<Animator>();
transform.position = new Vector3(0, 0, 0);
_spawnManagerAsteroid = GameObject.Find("AsteroidSpawner").GetComponent<SpawnManager>();
_spawnManagerEnemy = GameObject.Find("EnemySpawner").GetComponent<SpawnManager>();
if (_spawnManagerAsteroid == null || _spawnManagerEnemy == null) Debug.LogError("Spawner equals NULL");
}

void FixedUpdate()
{
HandleMovement();
HandleAnimation();
}

void HandleMovement()
{
//Movement
_inputX = Input.GetAxisRaw("Horizontal");
_inputY = Input.GetAxisRaw("Vertical");
Vector3 movement = new Vector3(_inputX, _inputY);
_rigidbody.AddForce(movement * _moveForce);

//Bounds
Vector3 position = transform.position;
if (position.y >= _yBounds * 1.5f) _rigidbody.AddForce(movement.x, movement.y - _yBounds * (_moveForce / 4), movement.z);
if (position.y <= -_yBounds * 1.5f) _rigidbody.AddForce(movement.x, movement.y + _yBounds * (_moveForce / 4), movement.z);
if (position.x >= _xBounds * 1.5f) _rigidbody.AddForce(movement.x - _xBounds * (_moveForce / 4), movement.y, movement.z);
if (position.x <= -_xBounds * 1.5f) _rigidbody.AddForce(movement.x + _xBounds * (_moveForce / 4), movement.y, movement.z);

}

void HandleAnimation()
{
if (_inputX < -0.2f)
{
_anim.SetBool("MovingLeft", true);
_anim.SetBool("MovingRight", false);
_anim.SetBool("Idle", false);

}
else if (_inputX > 0.2f)
{
_anim.SetBool("MovingLeft", false);
_anim.SetBool("MovingRight", true);
_anim.SetBool("Idle", false);

}
else if (_inputX > -0.2f && _inputX < 0.2f)
{
_anim.SetBool("MovingLeft", false);
_anim.SetBool("MovingRight", false);
_anim.SetBool("Idle", true);

}
}

public void Damage(int damage)
{
_health -= damage;
if (_health <= 0)
{
_lives--;
transform.position = new Vector3(0, 0, 0);
_health = _startingHealth;

}

if (_lives == 0) GameOver();

}

public void GameOver()
{
gameObject.SetActive(false);
_spawnManagerAsteroid.OnGameOver();
_spawnManagerEnemy.OnGameOver();

}

}

--

--