I’m trying to make player jump a fixed distance but it isn’t exact what I want.
Here is my code:
float jumpDistance = 1.5f;
bool jump;
Rigidbody2D rgb;
// Start is called before the first frame update
void Start()
{
rgb = GetComponent<Rigidbody2D>();
}
// Update is called once per frame
void Update()
{
if (Input.GetButtonDown("Jump"))
{
jump = true;
}
}
private void FixedUpdate()
{
if (jump)
{
jump = false;
rgb.AddForce(Vector2.up * GetJumpForce(), ForceMode2D.Impulse);
}
}
float GetJumpForce()
{
return Mathf.Sqrt(2f * Physics2D.gravity.magnitude * rgb.gravityScale * jumpDistance) * rgb.mass;
}
When I jump, my player can only get 1.44 height