最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

unity game engine - rapidly increasing gravity making the jump not work - Stack Overflow

programmeradmin2浏览0评论

Im making a flappy bird clone for practice and while making the jump feature im having issues because at the start the jump works but then gravity gets so strong that even if im still moving upwards jumping becomes increasingly more difficult (even on 0.25 or adding like 5k force onto the y coordinate)

looked up a few solutions and found that turning off gravity when the jump button is pressed but i dont think it works for RigidBody2D that well and another solution I tried is to bump up the upwards force but that only works as a short time solution idk where to put the code so im putting it here

using UnityEngine;

public class PlayerJump : MonoBehaviour
{
    public Rigidbody2D rb;
    public float jump = 10f;
    public float Flyspeed = 10f;

    // Update is called once per frame
   void start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
   
   
    void Update()
    {
       rb.AddForce(new Vector2(Flyspeed * Time.deltaTime, 0));
       
        if (Input.GetKeyDown("w"))
        {
           rb.AddForce(new Vector2(0, jump * Time.deltaTime));
           rb.useGravity = false;
        }
        else
        rb.useGravity = true;
    }
}

Im making a flappy bird clone for practice and while making the jump feature im having issues because at the start the jump works but then gravity gets so strong that even if im still moving upwards jumping becomes increasingly more difficult (even on 0.25 or adding like 5k force onto the y coordinate)

looked up a few solutions and found that turning off gravity when the jump button is pressed but i dont think it works for RigidBody2D that well and another solution I tried is to bump up the upwards force but that only works as a short time solution idk where to put the code so im putting it here

using UnityEngine;

public class PlayerJump : MonoBehaviour
{
    public Rigidbody2D rb;
    public float jump = 10f;
    public float Flyspeed = 10f;

    // Update is called once per frame
   void start()
    {
        rb = GetComponent<Rigidbody2D>();
    }
   
   
    void Update()
    {
       rb.AddForce(new Vector2(Flyspeed * Time.deltaTime, 0));
       
        if (Input.GetKeyDown("w"))
        {
           rb.AddForce(new Vector2(0, jump * Time.deltaTime));
           rb.useGravity = false;
        }
        else
        rb.useGravity = true;
    }
}
Share Improve this question asked Mar 31 at 3:50 MulankunasMulankunas 12 bronze badges New contributor Mulankunas is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct. 1
  • 1 Warning: Do not call rigidbody methods in Update, but call them in FixedUpdate. – shingo Commented Mar 31 at 6:39
Add a comment  | 

1 Answer 1

Reset to default 0

Instead of using forces, you can change the velocity of the rigidbody like this:

using UnityEngine;

public class PlayerJump : MonoBehaviour
{
    public Rigidbody2D rb;
    public float jumpForce = 10f;
    public float flySpeed = 10f;

    void Start()
    {
        rb = GetComponent<Rigidbody2D>();
    }

    void Update()
    {
        rb.velocity = new Vector2(flySpeed, rb.velocity.y);

        if (Input.GetKeyDown(KeyCode.W))
        {
            rb.velocity = new Vector2(rb.velocity.x, jumpForce);
        }
    }
}

This ensures a more direct approach, whithout the need to use a time-based solution.

Also, I corrected a typo in the Start() method—your original function was written as start() which prevented Unity from detecting automatically your Rigidbody2D.

发布评论

评论列表(0)

  1. 暂无评论