アニメーションクリップの変更

アニメーション
使い方、コンポーネントを獲得する。

private bool _isJumping;
private bool _isLeftMove;
private bool _isStop;
private Animator _animator;

void Start (){
    // コンポーネントを確保する。
    this._animator = this.GetComponent<Animator>();
    this._isLeftMove = true;
    this._isJumping = false;
    this._isStop = true;
}
void Update (){
    // 自分の速度を取得
    var v = this.rigidbody2D.velocity;

    // 条件で、true,falseを替える。
    if(v.x < -0.1f){
        this._isLeftMove = true;
        this._isStop = false;
    }
    else if (v.x > 0.1f){
        this._isLeftMove = false;
        this._isStop = false;
    }
    else{
        this._isStop = true;
    }
    this._animator.SetBool("isJump",this._isJumping);
    this._animator.SetBool("isStop",this._isStop);
    this._animator.SetBool("isLeftMove",this._isLeftMove);

}

f:id:yamakami21:20150420153905p:plain

codezine.jp

SetBoolを使わなくても、直接指定してあげることによって、アニメーションを変更することもできる。

public void OnCollisionEnter2D(Collision2D col){
	if(col.gameObject.name == "leftHitArea"){
		this._isLeftMove = false;
		this._animator.Play("hamster_move_right");
	}

	else if(col.gameObject.name == "rightHitArea"){
		this._isLeftMove = true;
		this._animator.Play("hamster_move_left");
	}

}

座標とかでも書き換えられそうだけど、直接指示してあげたほうがいいのだろう。