按下两个按钮时,动画不起作用-Unity 2d

发布于 2025-02-11 05:28:11 字数 1212 浏览 0 评论 0原文

我正在建立一个统一的2D项目,并且在为玩家制作动画时遇到了一些困难。

简而言之,玩家将有两个动作,分别是“步行”和“步行和射击”。

但是,当我将动画设置为“散步”时,它不会播放该动画。

问题在于必须按两个按钮播放动画。

我该怎么办来解决这个问题?

if (Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
    {
        _Anim.SetBool("Player_Walk", false);
        _Anim.SetBool("Player_Idle", false);
        _Anim.SetBool("Player_Idle_Shooting", false);
        _Anim.SetBool("Player_Walk_Shooting", true);
    }
    if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
    {
        _Anim.SetBool("Player_Walk", true);
        _Anim.SetBool("Player_Idle", false);
        _Anim.SetBool("Player_Idle_Shooting", false);
        _Anim.SetBool("Player_Walk_Shooting", false);  
    }

I'm building a 2d project in Unity and I'm having some difficulty animating the Player.

In short, the Player will have two actions, which are "Walk" and "Walk and Shoot".

However, when I set the animation to "Walk and Shoot" it doesn't play this animation.

The problem is in having to press two buttons to play the animation.

What can I do to resolve this issue?

if (Input.GetKeyDown(KeyCode.Space) && Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
    {
        _Anim.SetBool("Player_Walk", false);
        _Anim.SetBool("Player_Idle", false);
        _Anim.SetBool("Player_Idle_Shooting", false);
        _Anim.SetBool("Player_Walk_Shooting", true);
    }
    if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
    {
        _Anim.SetBool("Player_Walk", true);
        _Anim.SetBool("Player_Idle", false);
        _Anim.SetBool("Player_Idle_Shooting", false);
        _Anim.SetBool("Player_Walk_Shooting", false);  
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2025-02-18 05:28:11

我想您首先需要括号(因为(因为a&&& b || c不等于a&&&(b || c)),然后在第二个之前“ else”,然后“否则”:

if (Input.GetKeyDown(KeyCode.Space) && (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)))
[...]
else if(...)

我个性化会颠倒那个并写:

if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
{
    if(Input.GetKeyDown(KeyCode.Space){
        //first option
    } else {
        //second option
    }
}

对我来说看起来更简单

I suppose you need brackets in first if (because a && b || c is not equal to a && (b || c)) and then "else" before the second if:

if (Input.GetKeyDown(KeyCode.Space) && (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow)))
[...]
else if(...)

I personaly would invert that and write:

if (Input.GetKeyDown(KeyCode.W) || Input.GetKeyDown(KeyCode.UpArrow) || Input.GetKeyDown(KeyCode.A) || Input.GetKeyDown(KeyCode.LeftArrow) || Input.GetKeyDown(KeyCode.D) || Input.GetKeyDown(KeyCode.RightArrow) || Input.GetKeyDown(KeyCode.S) || Input.GetKeyDown(KeyCode.DownArrow))
{
    if(Input.GetKeyDown(KeyCode.Space){
        //first option
    } else {
        //second option
    }
}

That looks simpler to me

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文