我在让我的玩家跳入Unity

时间:2018-02-19 22:31:22

标签: c# unity3d

我目前正在为我的编码课创建我的最终项目,并且我遇到了如何跳跃的障碍。我使用了教程视频系列作为我项目的参考,这里是跳跃剧集的链接。 https://www.youtube.com/watch?v=05TCTrpGB-4&t=861s

我很可能在某处出现逻辑错误,但每件事情都很好。

继承我的代码:

private Rigidbody2D myRigidbody;

[SerializeField]
private float speed;

[SerializeField]
private Transform[] GroundPoints;

[SerializeField]
private float groundRadius;

[SerializeField]
private LayerMask WhatIsGround;

private bool isGrounded;

private bool Jump;

[SerializeField]
private float JumpForce;

// Use this for initialization
void Start () {
    myRigidbody = GetComponent<Rigidbody2D>();
}

// Update is called once per frame
void FixedUpdate () {
    isGrounded = IsGrounded();

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

    float horizontal = Input.GetAxis("Horizontal");
    HandleMovement(horizontal);

}

private void HandleInput()
{
    if (Input.GetKeyDown(KeyCode.Space))
    {
        Jump = true;
    }
}
//Moving left/right
private void HandleMovement(float horizontal) 
{

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

}



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;
}

1 个答案:

答案 0 :(得分:0)

HandleInput中没有澄清FixedUpdate。因此,代码未在更新中读取,使代码&#34;不可见&#34;。所以添加HandleInput就像我添加HandleMovement一样,允许读取该代码。

相关问题