繁体   English   中英

我正在尝试让我的 2D 统一角色跳跃

[英]I'm trying to make my 2D unity character jump

所以正如我所说,我试图让我的角色在 Unity 中跳跃,但是当我击中空间时什么都没有发生,而且 Unity 没有抛出任何错误。

public class Player : MonoBehaviour
{
    private Rigidbody2D myRigidbody;

    [SerializeField]
    public float jumpSpeed;

    private Animator myAnimator;

    [SerializeField]
    private float movementSpeed;

    private bool facingLeft;

    [SerializeField]
    private Transform[] groundPoints;

    [SerializeField]
    private float groundRadius;

    [SerializeField]
    private LayerMask whatIsGround;

    private bool isGrounded;

    private bool jump;

    [SerializeField]
    private float jumpForce;

    // Start is called before the first frame update
    void Start()
    {
        facingLeft = true;
        myRigidbody = GetComponent<Rigidbody2D>();
        myAnimator = GetComponent<Animator>();

    }

    // Update is called once per frame
    void FixedUpdate()
    {
        float horizontal = Input.GetAxis("Horizontal");
        Debug.Log(horizontal);

        isGrounded = IsGrounded();

        HandleMovement(horizontal);

        flip(horizontal);

    }

    private void HandleMovement(float horizontal)
    {

        myRigidbody.velocity = new Vector2(horizontal * movementSpeed, myRigidbody.velocity.y);

        myAnimator.SetFloat("speed", Mathf.Abs(horizontal));

        if (isGrounded && jump)
        {
            isGrounded = false;
            myRigidbody. AddForce(new Vector2(0, jumpForce));
        }

    }

    private void HandleInput()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            jump = true;
        }
    }

    private void flip(float horizontal)
    {
        if (horizontal < 0 && !facingLeft || horizontal > 0 && facingLeft)
        {
            facingLeft = !facingLeft;

            Vector3 theScale = transform.localScale;

            theScale.x *= -1;

            transform.localScale = theScale;
        }
    }

    private bool IsGrounded()
    {
        if (myRigidbody.velocity.y <= 0)
        {
            foreach (Transform point in groundPoints)
            {
                Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, groundRadius, whatIsGround);

                for (int i = 0; i < colliders.Length; i++)
                {
                    if (colliders[i].gameObject != gameObject)
                    {
                        return true;
                    }
                }
            }
        }
        return false;
    }

}

我一直在关注这个教程,他在跳,但我的不是。 我认为这可能与我设置接地点的方式有关,但我不确定这是代码中的错误还是 Unity 中的错误。

您没有在任何地方调用 HandleInput 。

添加到固定更新

void FixedUpdate()
{
    float horizontal = Input.GetAxis("Horizontal");
    Debug.Log(horizontal);

isGrounded = IsGrounded();

HandleMovement(horizontal);

flip(horizontal);

HandleInput(); // add this so you are sampling input

}

暂无
暂无

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

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