我的角色在Unity2D中做了意想不到的第三次跳跃

时间:2016-01-27 00:40:59

标签: c# unity3d 2d controllers unity5

您好!

我目前正在使用Unity引擎中的2D平台游戏,在那里我创建了一个可以执行双重跳跃的角色。
一开始这一切都很好,但我现在意识到有一个破坏游戏的错误使他能够预先形成第三次跳跃(出于某种原因)。而且我不能为它找出问题所在。

PlayerController.cs

ParseConfig.getInBackground(new ConfigCallback(){
                             public void done(ParseConfig config, ParseException e){
                                 Object ratio = config.get("lastRatio");
                                 Toast.makeText(RequestServiceActivity.this, "Ratio: "+ ratio, Toast.LENGTH_LONG).show();

感谢所有帮助!

2 个答案:

答案 0 :(得分:1)

void JumpLogic()中,您正在调用

currentJumps++;
CheckGroundCollision();

第一次按下跳转键时,currentJumps变为1(currentJumps ++),但isGrounded再次变为真(因为物理尚未在当前帧中应用,并且角色仍在接触地面层面罩)。因此,下次调用JumpLogic()时,currentJumps会因为if (isGrounded) currentJumps = 0;而在运行跳转键代码之前重置为零。

我建议删除CheckGroundCollision();void JumpLogic()的最后一次通话。 无论如何,它们都在Update()内部调用,其顺序看起来像是可行的。

我希望有所帮助!

<强>更新 我刚刚注意到你还在CheckGroundCollision();内部第二次打电话给Update()。那也是重置跳转变量。

尝试修改原始代码:

void Update() {
    CheckGroundCollision();
    JumpLogic();

    movement = new Vector3(Input.GetAxis("Horizontal") * speed, 0, 0);
    movement *= Time.deltaTime;
    transform.position += movement;

    //** NOTE- REMOVED THIS LINE
}

...

void JumpLogic()
{

    if (isGrounded)
        currentJumps = 0;

    if (Input.GetButtonDown("Jump") && currentJumps < maxJumps)
    {
        GameObject newJumpSound = (GameObject)GameObject.Instantiate(jumpSound, (Vector2)transform.position, transform.rotation);
        GameObject newJumpEffect = (GameObject)GameObject.Instantiate(jumpEffect, (Vector2)transform.position - new Vector2(0, 0.25f), transform.rotation);
        GameObject.Destroy(newJumpEffect, 0.2f);
        GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
        GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
        currentJumps++;

        //** NOTE- REMOVED THIS LINE
    }
}

我希望有所帮助!

答案 1 :(得分:0)

我建议让对撞机向玩家控制器指示isGrounded的状态而不是查询对撞机。例如,继续andeart's建议,您的代码可能看起来更像以下

void Update() {
    JumpLogic();

    movement = new Vector3(Input.GetAxis("Horizontal") * speed, 0, 0);
    movement *= Time.deltaTime;
    transform.position += movement;
}

//let the collider indicate to the player controller when
//a collision occurs, then determine if this collision is relevant
void OnCollisionEnter2D(Collision2D coll) {
    if(coll.IsTouchingLayers(groundAbles))    
      isGrounded = true;

    // or do some other check using layers or tags ....
}

void JumpLogic()
{
    if (isGrounded)
        currentJumps = 0;

    if (Input.GetButtonDown("Jump") && currentJumps < maxJumps)
    {
        GameObject newJumpSound = (GameObject)GameObject.Instantiate(jumpSound, (Vector2)transform.position, transform.rotation);
        GameObject newJumpEffect = (GameObject)GameObject.Instantiate(jumpEffect, (Vector2)transform.position - new Vector2(0, 0.25f), transform.rotation);
        GameObject.Destroy(newJumpEffect, 0.2f);
        GetComponent<Rigidbody2D>().velocity = new Vector2(GetComponent<Rigidbody2D>().velocity.x, 0);
        GetComponent<Rigidbody2D>().AddForce(new Vector2(0, 10), ForceMode2D.Impulse);
        currentJumps++;
    }
}

请参阅Collider2D.IsTouchingLayers,尤其是底部段落,这意味着IsTouchingLayers方法可能无法提供与对撞机相关的最精确结果。