admin管理员组文章数量:1355564
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
1 Answer
Reset to default 0Instead 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.
本文标签: unity game enginerapidly increasing gravity making the jump not workStack Overflow
版权声明:本文标题:unity game engine - rapidly increasing gravity making the jump not work - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1743968551a2570339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论