简体   繁体   English

unity 2D 跳转按钮 Onclick Animation 无法正常工作

[英]Unity 2D Jump Button Onclick Animation does not work properly

I'm sitting for hours at the same problem.我在同一个问题上坐了几个小时。 I'm doing a WebGL game, which should be possible to play with mobile control (touch) or with keyboard when playing on PC.我正在做一个 WebGL 游戏,在 PC 上玩时应该可以用移动控制(触摸)或键盘玩。 At the beginning of the game, Player is asked if he plays with keyboard or mobile, which then changes the UI to a Joystick and Buttons, when he plays mobile.在游戏开始时,会询问玩家他玩的是键盘还是移动设备,然后当他玩移动设备时,它会将 UI 更改为操纵杆和按钮。

In Unity Animator, the Players Animations for Movement do have bools, which control the animations, so fe when anim.SetBool("Jump", true) the Jump Animation starts.在 Unity Animator 中,Players Animations for Movement 确实有控制动画的布尔值,所以当 anim.SetBool("Jump", true) 跳转 Animation 开始时。 When false and Player has no Movement, Idle Animation plays, or when false and Movement, Walking Animation plays.当 false 并且 Player 没有移动时,播放 Idle Animation,或者当 false 和 Movement 时,播放 Walk Animation。

This all works fine when I press 'Space'.当我按“空格”时,这一切都很好。 When I press Space Jump() is invoked.当我按下 Space Jump() 时被调用。

The Players movement is fine when playing with keyboard, but I struggle hard to get a jumping button working.使用键盘演奏时,玩家的动作很好,但我很难让跳跃按钮起作用。 The character is jumping, but for any reasons the animation does only work when the Input is from the Keyboard.字符正在跳跃,但出于任何原因,animation 仅在输入来自键盘时才起作用。 Also both, the Button and the Keyboard just invoke the same function, please have a look:同样,按钮和键盘只是调用相同的 function,请看一下:

using UnityEngine.UI;

public class PlayerMovement : MonoBehaviour
{
    //...
    public bool isMobile;
    public Joystick joystick;

    GameObject rend;

    public Button buttonJump;

    void Start()
    {
        // HERE Jump() IS INVOKED, WHEN THE JUMP BUTTON IS PRESSED!
        buttonJump.onClick.AddListener(Jump);
    }

    void Update()
    {
        PlayerJump();
        CheckIsGrounded();
    }

    private void FixedUpdate()
    {
        PlayerWalk();
    }

    void PlayerJump()
    {
        if (isGrounded)
        {
            if ( Input.GetKey(KeyCode.Space))
            {
                Jump();
            }
        }
    }

    //WHEN INVOKED VIA JUMP BUTTON, THE PLAYER MOVES UP FINE, BUT THE JUMP ANIMATION DOES NOT START!
    public void Jump()
    {
        if (isGrounded)
        {
            anim.SetBool("Jump", true);
            jumped = true;
            myBody.velocity = new Vector2(myBody.velocity.x, jumpPower);
        }
    }

    void CheckIsGrounded()
    {
        isGrounded = Physics2D.Raycast(groundCheckPosition.position, Vector2.down, 0.1f, groundLayer);

        if (isGrounded)
        {
            if (jumped) 
            {
                anim.SetBool("Jump", false);

                jumped = false;
            }
        }
    }
}

I got a workarround, I think there is something like a bug in Unity itself (using 2019.3.8f1).我有一个解决方法,我认为 Unity 本身存在类似错误(使用 2019.3.8f1)。 I simply created a own Script, only for the one Jump Button, when it is pressed, sets the bool for the Animator and adds the velocity to the rigidbody, exactly the same, as I wrote in the old code, but in the new script it works.我只是创建了一个自己的脚本,仅用于一个 Jump Button,当它被按下时,为 Animator 设置布尔值并将速度添加到刚体,与我在旧代码中编写的完全相同,但在新脚本中有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM