簡體   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