Skip to content

スクリプト上からのアニメーション切り替えに関しまして #96

@kanata3

Description

@kanata3

お世話になります。

下記の様なコードを *_Control に付与し方向キーの入力でアニメーションの切り替えを行おうとしております。
(長い為、一部コードを削除して掲載しています)

enum PlayerAnimation
{
Back=0,
Default=1,
Front = 2,
Left = 3,
Right = 4
}

public class Player : MonoBehaviour {

private Script_SpriteStudio_PartsRoot rootAnimation;
private PlayerAnimation animationNow    = PlayerAnimation.Front;

// Use this for initialization
void Start () {
    Script_SpriteStudio_LinkPrefab link = this.GetComponent<Script_SpriteStudio_LinkPrefab>();

    Debug.Log(((GameObject)link.LinkPrefab).name);

    rootAnimation = ((GameObject)link.LinkPrefab).GetComponent<Script_SpriteStudio_PartsRoot>();

    rootAnimation.AnimationStop();
    rootAnimation.AnimationPlay((int)PlayerAnimation.Default, 1, -1, 1.0f, (Script_SpriteStudio_PartsRoot.PlayStyle.NORMAL), "", int.MaxValue, "", int.MaxValue);
}

// Update is called once per frame
void Update () {
    // 右・左
    float x = Input.GetAxisRaw("Horizontal");

    // 上・下
    float y = Input.GetAxisRaw("Vertical");

    // 移動する向きを求める
    Vector2 direction = new Vector2(x, y).normalized;

    // アニメ変更
    Animation(x, y);
}

// 方向によりアニメ変更
void Animation(float x, float y)
{
    PlayerAnimation next_animation;

    float x_abs = Mathf.Abs(x);
    float y_abs = Mathf.Abs(y);

    if( x_abs==0 && y_abs==0 )
    {
        next_animation = PlayerAnimation.Default;
    }
    else
    {
        if( x_abs==y_abs )
        {
            if (x > 0)
            {
                next_animation = PlayerAnimation.Right;
            }
            else
            {
                next_animation = PlayerAnimation.Left;
            }
        }
        else if( x_abs>y_abs )
        {
            if( x > 0 )
            {
                next_animation  = PlayerAnimation.Right;
            }
            else
            {
                next_animation = PlayerAnimation.Left;
            }
        }
        else
        {
            if (y > 0)
            {
                next_animation = PlayerAnimation.Back;
            }
            else
            {
                next_animation = PlayerAnimation.Front;
            }
        }
    }

    if( animationNow!= next_animation)
    {
        //Debug.Log(next_animation);
        animationNow = next_animation;

        Debug.Log("before: " + rootAnimation.AnimationNo);

        rootAnimation.AnimationStop();
        rootAnimation.AnimationPlay((int)animationNow, 1, -1, 1.0f, (Script_SpriteStudio_PartsRoot.PlayStyle.NORMAL), "", int.MaxValue, "", int.MaxValue);

        Debug.Log("after: " + rootAnimation.AnimationNo);
    }
}

}


Start()時の下記コードで *_Control の元になっている prefab のオブジェクトは参照できていることを確認しています。

Debug.Log(((GameObject)link.LinkPrefab).name);

また、Update()時の下記コードでは Before と After で AnimationNo が変わっている事を確認しています。

Debug.Log("before: " + rootAnimation.AnimationNo);

rootAnimation.AnimationStop();
rootAnimation.AnimationPlay((int)animationNow, 1, -1, 1.0f, (Script_SpriteStudio_PartsRoot.PlayStyle.NORMAL), "", int.MaxValue, "", int.MaxValue);

Debug.Log("after: " + rootAnimation.AnimationNo);

ですが、表示側でアニメーションが切り替わってくれません。

その代わり、プレビューを終えると最後に AnimationPlay() で切り替えたアニメに
*_Control の元となっている Prefab の Animation Name の値がUnityのUI上で替わっています。

こちら本来はどのように変更すべき物でしょうか?

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions