![](/img/trans.png)
[英]When I press space, my character doesn't always jump in my Unity2D project
[英]Hi, my 2D game character teleport a little bit higher, and then he come down very slow, like a leaf when i press the jump button
如果我繼續按下按鈕,它會繼續上升。 當我刪除用於向左移動的腳本時,它工作得很好,但是當我添加它時,我遇到了這個問題。 這是我的代碼
` { Rigidbody2D rb;
float dirX;
float jumpForce = 300f;
float moveSpeed = 5f;
void Start()
{
rb = GetComponent<Rigidbody2D>();
}
void Update()
{
dirX = CrossPlatformInputManager.GetAxis("Horizontal");
rb.velocity = new Vector2(dirX * 10, 0);
if (CrossPlatformInputManager.GetButtonDown("Jump"))
DoJump();
}
void DoJump()
{
if (rb.velocity.y == 0)
rb.AddForce(new Vector2(0, jumpForce), ForceMode2D.Force);
}
}`
rb.velocity = new Vector2(dirX * 10, rb.velocity.y);
rb.AddForce(new Vector2(rb.velocity.x, jumpForce), ForceMode2D.Force);
是的,只需要進行這些更改
rb.velocity = new Vector2(dirX * 10, rb.velocity.y);
rb.AddForce(new Vector2(rb.velocity.x, jumpForce), ForceMode2D.Force);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.