【问题标题】:Why aren't the animations working for this 2D unity character controller script为什么动画不适用于此 2D 统一角色控制器脚本
【发布时间】:2020-05-15 23:22:55
【问题描述】:

我有一些代码可以让玩家上下左右移动。

public class playerMovement : MonoBehaviour
{
    public float moveSpeed = 5f;
    public Rigidbody2D rb;
    public Animator animator;
    public SpriteRenderer sr;

    Vector2 movement;
    bool walking = false;

    private void Update()
    {
        //Inputs
        movement.x = Input.GetAxisRaw("Horizontal");
        movement.y = Input.GetAxisRaw("Vertical");

        if (Input.GetKey("left") || Input.GetKey("right") || Input.GetKey("up") || Input.GetKey("down")
        {
            walking = true;
        }
        else
        {
            walking = false;
        }

        Animate();
    }

    private void Animate()
    {
        if (Input.GetKey("down") && walking == false)
        {
            sr.flipX = false;
            animator.Play("idle_front");
        }
        else if (Input.GetKey("down") && walking)
        {
            sr.flipX = false;
            animator.Play("walk_front");
        }
        else if (Input.GetKey("up") && walking == false)
        {
            sr.flipX = false;
            animator.Play("idle_back");
        }
        else if (Input.GetKey("down") && walking)
        {
            sr.flipX = false;
            animator.Play("walk_back");
        }
        else if (Input.GetKey("left") && walking == false)
        {
            sr.flipX = false;
            animator.Play("idle_side");
        }
        else if (Input.GetKey("down") && walking)
        {
            sr.flipX = false;
            animator.Play("walk_side");
        }
        else if (Input.GetKey("right") && walking == false)
        {
            sr.flipX = true;
            animator.Play("idle_side");
        }
        else if (Input.GetKey("right") && walking)
        {
            sr.flipX = true;
            animator.Play("walk_side");
        }

    }

    public void FixedUpdate()
    {
        //Movement
        rb.MovePosition(rb.position + movement * moveSpeed * Time.fixedDeltaTime);
    }
}

动作运行良好,但角色只是停留在空闲状态。 我尝试将Input.GetKey("left") || Input.GetKey("right") || Input.GetKey("up") || Input.GetKey("down") 中的get 键切换为Input.GetKeyDown()Input.GetKeyUp(),但这些都不起作用。

我也尝试过使用混合树,但我不知道该怎么做。

我对 Unity 还很陌生,所以一个简单的答案会很棒。

【问题讨论】:

标签: c# unity3d animation


【解决方案1】:

打开动画窗口

创建“状态”

将动画剪辑设置为“运动”

点击左上角的“参数”标签

点击参数旁边的加号

点击“触发”

为你的触发器命名

右键单击“任何状态”并进行转换以将一条线拖动到您创建的新“状态”

单击“任何状态”和您创建的“状态”之间的线

在“条件”下按加号并添加您刚刚创建的触发器

在代码中编写

animator.SetTrigger("triggername");

当你想转换到前面提到的动画状态时 重复所有动画,从“任何状态”链接它们

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-09-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    相关资源
    最近更新 更多