2017-08-30 47 views
0

我正在制作2D游戏。我的问题是,在玩游戏时,如果玩家持有跳跃并在BoxCollider2D下,玩家将不会掉落,直到他们释放跳跃。统一5:刚体2d坚持天花板,速度向上移动

我的播放器游戏对象由一个精灵渲染器,一个动态的刚体body2d与重力和boxcollider2d。

这里是我运动脚本:

1:

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

public class JumpScript : MonoBehaviour { 

    [Range(1, 10)] 
    public float jumpVelocity; 

    [Range(0,10)] 
    public float speed; 
    private bool jumpQueue = false; 
    private bool boolin=false; 
    void Update() 
    { 

     //Friggin fall, loser 

     //Jumping 
     ///* 
     if (Input.GetButton("Jump")&& GetComponent<Rigidbody2D>    ().velocity.y==0) 
     { 
      GetComponent<Rigidbody2D>().velocity = Vector2.up *  jumpVelocity; 
     } 
     //*/ 
     //jumpQueue? 
     /* 
     if(Input.GetButtonDown("Jump")) 
     { 
      jumpQueue = true; 
     }*/ 
     //Right Movement 
     if (Input.GetKey(KeyCode.D)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(1*speed,   GetComponent<Rigidbody2D>().velocity.y); 
      boolin = true; 
     } 
     if(Input.GetKeyUp(KeyCode.D)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(0,  GetComponent<Rigidbody2D>().velocity.y); 
      boolin = false; 
     } 
     //Left Movement 
     if (Input.GetKey(KeyCode.A)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(-1*speed,  GetComponent<Rigidbody2D>().velocity.y); 
      boolin = true; 
     } 
     if (Input.GetKeyUp(KeyCode.A)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(0,  GetComponent<Rigidbody2D>().velocity.y); 
      boolin = false; 
     } 
     //No movement? 
     if (Input.GetKey(KeyCode.D) && Input.GetKey(KeyCode.A)) 
     { 
      GetComponent<Rigidbody2D>().velocity = new Vector2(0,  GetComponent<Rigidbody2D>().velocity.y); 
      boolin = false; 

     } 

     //Time to handle animation, boios. 
     Rigidbody2D rb = GetComponent<Rigidbody2D>(); 

     bool schwomp = false; 
     bool schwift = false; 
     if(rb.velocity.y>0) 
     { 
      schwomp = true; 
     } 
     if(rb.velocity.y<0) 
     { 
      schwift = true; 
     } 
     Animator anim = GetComponent<Animator>(); 
     if (boolin) 
     { 
      anim.SetInteger("Boolin", 1); 
      /*if (!anim.GetBool("expand")) 
      { 
       anim.SetBool("expand", true); 
       anim.Play("running"); 
      }*/ 
     } 
     else 
     { 
      anim.SetInteger("Boolin", 0); 
      /* 
      if(anim.GetBool("expand")) 
      { 
       anim.SetBool("expand", false); 
       anim.Play("Idle"); 
      }*/ 
     } 
     if(schwomp) 
     { 
      //anim.SetInteger("Boolin", 2); 
     } 
     if(schwift) 
     { 
      //anim.SetInteger("Boolin", 3); 
     } 
    } 
} 

using System.Collections; 
using System.Collections.Generic; 
using UnityEngine; 

公共类BetterJumper:MonoBehaviour {

public float fallMultiplier = 2.5f; 
public float lowJumpMultiplier = 2f; 

Rigidbody2D rb; 

void Awake() 
{ 
    rb = GetComponent<Rigidbody2D>(); 

} 

void Update() 
{ 
    if(rb.velocity.y<0) 
    { 
     rb.velocity += Vector2.up*Physics2D.gravity.y*(fallMultiplier-1)*Time.deltaTime; 

    } 
    else if(rb.velocity.y>0&&!Input.GetButton("Jump")) 
    { 
     rb.velocity += Vector2.up * Physics2D.gravity.y * (lowJumpMultiplier - 1) * Time.deltaTime; 
    } 
} 
} 

非常感谢你提前!

+0

请花点时间正确设置您的代码的格式。 – Soviut

+0

你写了什么问题,但没有提供什么样的预期结果.... – Programmer

回答

3

您正在使用Input.GetKey(),它会在每一帧中轮询密钥。这意味着越长越好,速度越快。你已经有效地建立了一个喷气背包,而不是一个跳跃力量。

您应该使用Input.GetKeyDown(),只有在按下某个键时才会触发一次,然后必须再次释放并再次按下才能重新触发。然后你需要使用一个足够强的垂直力量使用RigidBody.AddForce()来使角色跳跃,而不是连续增加速度。

此外,当脚本被唤醒或启动时,您应该真的缓存您的GetComponent<Rigidbody2D>()调用的结果,以免连续调用它;这些调用中的每一个都需要处理时间。另外,您应该使用FixedUpdate()进行物理学。

public class ExampleClass : MonoBehaviour { 
    public float thrust; 
    public Rigidbody rb; // make the rigidbody variable available anywhere in the class 

    void Start() { 
     // cache the rigidbody component to the variable once on start 
     rb = GetComponent<Rigidbody>(); 
    } 

    void FixedUpdate() { 
     // use the variable reference from now on rather than making GetComponent calls 
     rb.AddForce(transform.up * thrust); 
    } 
} 
+0

感谢您的帮助!我一定会尝试这个! – TinFellow

0

Soviut的回答很好地解释了它,但还有更多的你需要知道。您直接操纵速度,这会覆盖任何力的影响,包括重力(尽管您正在手动应用)。请参阅documentation

正如Soviut的回答中所证明的,您应该使用力量和冲动,让物理引擎确定速度。你应该直接设定速度的时候是当你建立一个故意不切实际的物理模拟(即复古平台)时。即使在这种情况下,请记住,这样做会创造更多的工作,因为您需要考虑每一件创造运动的小东西。这意味着你基本上正在重新发明物理引擎。

+0

有趣的是,我试图创造一个复古的平台,哈哈。 – TinFellow

+0

哇,有多讽刺。在任何情况下,如果你限制最大速度,并且不要过度使用你施加的力量,你可以达到复古平台游戏的感觉。祝你好运。 –

+0

这是比我可以应用于其他情况的解决方案更多的解决方法。但是,我确实使用了光线投射来确定玩家是否接地。这样一来,如果他在地面上,他现在只会跳起来。谢谢您的帮助! – TinFellow