AI运动与动画C#统一

时间:2018-08-14 11:55:30

标签: unity3d

我正在尝试学习制作简单的2D游戏,但是我有一个问题。我的AI可以朝随机方向行走,但动画不会改变。移动时,动画处于空闲动画中。

void Start () {

    anim = GetComponent<Animator>();
    myrigbody = GetComponent<Rigidbody2D>();
    waitCounter = walkTime;
    walkCounter = walkTime;

    ChooseDirections();
}


void Update () {


    if (isWalking)
    {

        walkCounter -= Time.deltaTime;

        switch (walkdirection)
        {
            case 0:
                myrigbody.velocity = new Vector2(0, moveSpeed);
                break;
            case 1:
                myrigbody.velocity = new Vector2(moveSpeed, 0);
                break;
            case 2:
                myrigbody.velocity = new Vector2(0, -moveSpeed);
                break;
            case 3:
                myrigbody.velocity = new Vector2(-moveSpeed, 0);
                break;
        }

        if (walkCounter < 0)
        {
            isWalking = false;
            waitCounter = waitTime;
        }
    }

    else
    {
        waitCounter -= Time.deltaTime;
        myrigbody.velocity = Vector2.zero;

        if (waitCounter < 0)
        {
            ChooseDirections();
        }
    }
}

public void ChooseDirections()
{
    walkdirection = Random.Range(0,4);
    isWalking = true;
    walkCounter = walkTime;
}

有没有办法让我的AI动画面对他要去的地方?看来他不会跟随混合树。

1 个答案:

答案 0 :(得分:0)

使用Animator控制器; See this link

如果您正在研究自上而下的游戏,并且想让Game Object面对他的去向,this link可以为您提供帮助。