Book Image

Unity Animation Essentials

By : Alan Thorn
Book Image

Unity Animation Essentials

By: Alan Thorn

Overview of this book

Table of Contents (14 chapters)
Unity Animation Essentials
About the Author
About the Reviewers

Scripting with Mecanim Blend Trees

We're now almost ready to animate the player character from a script. We just need to define the newly created script file. Consider the following code sample, and then a deeper explanation will follow:

using UnityEngine;
using System.Collections;

public class CharControl : MonoBehaviour 
  //Animator Controller
  private Animator ThisAnimator = null;

  //Float names
  private int HorzFloat = Animator.StringToHash("Horz");
  private int VertFloat = Animator.StringToHash("Vert");
  void Awake()
    //Get animator component on this object
    ThisAnimator = GetComponent<Animator>();

  // Update is called once per frame
  void Update () 
    //Read player input
    float Vert = Input.GetAxis("Vertical");
    float Horz = Input.GetAxis("Horizontal");

    //Set animator floating point values
    ThisAnimator.SetFloat(HorzFloat, Horz, 0.2f, Time.deltaTime);
    ThisAnimator.SetFloat(VertFloat, Vert, 0.2f, Time.deltaTime);