An Animated Character with Independent States and Transitions

For this project, I have animated a character with eight independent states. The idle state is the default state of the character, and all states transition back to the idle state when complete. The animation states are: Idle, walk, take damage, player death, attack right arm, attack left arm, kick left leg, and kick right leg. Adding an animated character improves the game by providing player movement mechanics. This allows players to interact with the game space in a way that is familiar. Players may be more likely to accept that they are playing a game if they are familiar with the mechanics and are able to interact with other game elements through a realistic character (as opposed to, say, a rolling ball). Animation states can be triggered by pressing a button on the screen.

 

The Animator Controller

The animator controller handles the animation states and transitions. Transitions between states occur by toggling the parameters on the left-hand side of the animator. The character asset that I downloaded from the Unity Asset Store contains many animations, however for the sake of this project, I chose to use only 8. Because I did not use all the animations, I had to create a new animator controller to handle the animation states and I had to create parameters that defined the transitions between states.

 

Parameters

Most animation states are called by triggers. For example, when the “Attack 1” trigger is called, the animator transitions to one of the attack state animations, and then returns to the idle state. I labeled each attack trigger “Attack ” + 1-4 for future projects. I did this so that I can create a script that – when the game receives input from the player – a random number between 1 and 4 will be generated and a corresponding attack trigger will be called. I did this in order to add variety to my future game and improve the overall aesthetics. A player may find the fact that the character has different attack animations interesting. There are also triggers for when the character receives damage, and when the player dies. In the animator, I chose to use a Boolean value for running called “isRunning”. I did this to ensure that the walking animation state did not transition back to the idle state as long as “isRunning” is true.

Buttons

Scripting the buttons was quite simple. The GUIButtonAnimator script is attached to the canvas game object, which handles UI elements such as buttons and text. When the player clicks on a button, it passes the name of the trigger or Boolean value to either the clickToAnimateTrigger() or clickToAnimateBool() methods. The or clickToAnimateBool() method can be called to both turn the animator Boolean value on or off. The method also deactivates or activates a set of buttons that are stored in the buttonsToDeactivate list. This ensures that the user can’t call a different animation while the player is walking.

Button Control Script:

using UnityEngine;
using System.Collections;
using UnityEngine.UI;

public class GUIButtonAnimator : MonoBehaviour {
    
    public Animator playerAnimator;
    public Button[] buttonsToDeactivate;

    public void clickToAnimateTrigger (string animatorTriggerLabel) {
        SetTrigger(animatorTriggerLabel); 
    }

    public void clickToAnimateBool (string animatorBoolLabel) {
        SetBool(animatorBoolLabel);
    }


    public void SetTrigger (string triggerLabel) {
        playerAnimator.SetTrigger(triggerLabel);
    }

    public void SetBool (string boolLabel) {
        
        bool currentState = playerAnimator.GetBool (boolLabel);
        if (!currentState) {
            playerAnimator.SetBool (boolLabel, true);
            foreach (Button button in buttonsToDeactivate) {
                button.interactable = false;
            }
        } else {
            playerAnimator.SetBool(boolLabel, false);
            foreach (Button button in buttonsToDeactivate) {
                button.interactable = true;
            }
        }
    }
}

Adding a Camera Shake Effect

While I was building the game, I thought of an interesting effect to make the animations more realistic. I had originally thought that shaking the camera transform slightly when one of the animations played would create an effect that the character has successfully attacked an enemy or that the character has some physical force. I attached a script to the character game object that would shake the camera transform at a certain intensity. In my 2D platformer project, I learned that methods could be called at a certain point in an animation. In the case of this game, when an animation occurred, a float would be passed to a camera shake method called ShakeCamera(). This value would be assigned to a float value called shakeDuration which would decrease over time. As long as the shakeDurantion was bigger than 0, the camera would shake.

Camera Shake Script:

using UnityEngine;
using System.Collections;

public class CameraShake : MonoBehaviour {

    public Transform camTransform;
    
    public float shakeDuration = 0f;
    
    public float shakeAmount = 0.7f;
    public float decreaseFactor = 1.0f;

    Vector3 originalPos;

    public void ShakeCamera (float duration) {
        shakeDuration = duration;
    }
    
    void Awake(){
        if (camTransform == null){
            camTransform = GetComponent(typeof(Transform)) as Transform;
        }
    }
    
    void OnEnable(){
        originalPos = camTransform.localPosition;
    }

    void Update(){
        if (shakeDuration > 0){
            camTransform.localPosition = originalPos + Random.insideUnitSphere * shakeAmount;
            shakeDuration -= Time.deltaTime * decreaseFactor;
        } else {
            shakeDuration = 0f;
            camTransform.localPosition = originalPos;
        }
    }
}

The script worked well, however the animations that came with the character asset (that I got for free from the Unity Asset Store) were set to read-only and therefore could not be edited. For this reason, whenever I built the game or saved the project, the methods were removed from the animation. Nonetheless, it was a learning opportunity as I go to experiment with calling methods from an animation and I now understand the limitations of using free assets from Unity’s Asset Store.

Future Projects and Conclusion

While I was building this project, I began to think of future projects and how animations could be applied to a larger game. Animations are critical to creating a realistic player movement effect. For the next project, I will need to move a nav-mesh agent between points and to a mouse click. The animation states that I used in this project, specifically the idle state and the walking state, will need to be used to create the illusion that the nav-mesh agent is physically walking in the game space. Otherwise, the character would simply slide across the screen in a way that does not seem realistic.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s