如何创建Unity 2D敌方AI不同方向的动画?

时间:2018-07-25 00:05:09

标签: c# unity3d

因此,基本上我想做的是创建一个动画脚本,该脚本不仅仅像Up(0,1),Down(0,-1),Left(-1,0),Right(1)这样简单的方向,0)(这些数字是矢量),但要具有方向范围,以填充这些方向之间的间隙并播放上,下,左,右的主导方向。 因此,下面的每个四分之一部分都应设置为动画。

enter image description here

我创建了一些脚本来尝试实现这一目标,我想出了一个不完美的脚本,但是当游戏运行时在场景编辑器中拖动敌人时它确实起作用了。当使用他们的动作脚本进行动作时,它不起作用。动画脚本位于我如何移动敌人的下方和下方。

下面是我计算敌人移动方向的方法:

IEnumerator directionFinder()
{
   // while (true)
    //{
        Vector3 previousPosition = transform.position;
        yield return new WaitForSeconds(1/10);
        currentPosition = transform.position;
        currentPosition.x = currentPosition.x - previousPosition.x;
        currentPosition.y = currentPosition.y - previousPosition.y;
        currentPosition.z = 0f;

        angle = (Mathf.Atan2(currentPosition.y, currentPosition.x) * Mathf.Rad2Deg);

        if(angle < 0)
        {
            angle = Mathf.Abs(angle) + 180;
        }

   // } 
}

这就是计算要使用哪个动画的内容:

 void animationDirection()
{

    if (angle == 0)
    {
        anim.SetInteger("Move", currentAnim);
    }
    if (angle > 45 && angle <135)//W
    {
        anim.SetInteger("Move", 1);
        currentAnim = 1;
    }
    else if (angle > 135 && angle < 225)//A
    {
        anim.SetInteger("Move", 2);
        currentAnim = 2;
    }
    else if (angle > 225 && angle < 315)//S
    {
        anim.SetInteger("Move", 3);
        currentAnim = 3;
    }
    else if((angle < 45 && angle >= 0) || (angle > 315 && angle <= 0))//D
    {
        anim.SetInteger("Move", 4);
        currentAnim = 4;
    }
}

使用下面的代码,它将移动到通过我的其余代码选择的航路点,该代码将循环遍历一组航路点,并在到达时改变航路点。

transform.position = Vector2.MoveTowards(transform.position, waypoints[waypointNUmber].transform.position, speed * Time.deltaTime);

以下,我尝试了另一种方法,但是它不起作用,并创建了怪异的越野车动画:

 IEnumerator directionalAnimation()
{
    while (true)
    {
        Debug.Log("working");
        Vector2 previousPosition = transform.position;
        yield return new WaitForSeconds(1/10);
        Vector2 currentPosition = transform.position;
        Vector2 direction = (previousPosition - currentPosition).normalized;

        float moveX = direction.x;
        float moveY = direction.y;

        if (moveX < 0)
        {
            moveXNegative = true;
            moveX = Mathf.Abs(moveX);
        }
        if (moveY < 0)
        {
            moveYNegative = true;
            moveY = Mathf.Abs(moveY);
        }

        if (direction.magnitude != 0)
        {
            if (moveX > moveY)
            {
                if (moveXNegative)
                {
                    //left
                    Walking = true;
                    anim.SetInteger("Move", 2);
                    moveYNegative = false;
                }
                else
                {
                    //Right
                    Walking = true;
                    anim.SetInteger("Move", 4);
                    moveYNegative = false;
                }
            }
            else if (moveY > moveX)
            {
                if (moveYNegative)
                {
                    //Down
                    Walking = true;
                    anim.SetInteger("Move", 3);
                }
                else
                {
                    //Up
                    Walking = true;
                    anim.SetInteger("Move", 1);
                }
            }
        }
        else
        {
            source.Pause();
            Walking = false;
        }

        if (Walking)
        {
            source.UnPause();
        }
    }


}

0 个答案:

没有答案