Just making a simple platform game, with a script to handle movement and jumping by the player. The player has a collider on the bottom as a trigger to detect when it collides with anything tagged Ground. Everything runs alright. except the jumps seem floaty. I want to make the player fall down from a jump faster and sooner than they currently do, but increasing the gravity makes the jump height shorter, which i dont want
the code is as follows:
public class garbageEngine : MonoBehaviour {
Rigidbody2D rb;
float speed;
public bool isgrounded = false;
float verticalvelocity;
float gravity = 14f;
float jumpforce = 20f;
void Start()
{
rb = gameObject.GetComponent<Rigidbody2D>();
speed = 10f;
}
void OnTriggerEnter2D(Collider2D collision)
{
if (collision.gameObject.tag == "Ground")
{
isgrounded = true;
}
}
void Update()
{
Debug.Log("vertical velocity is " + verticalvelocity);
rb.velocity = new Vector2(Input.GetAxisRaw("Horizontal") * speed, rb.velocity.y);
if(isgrounded == true)
{
verticalvelocity = -gravity * Time.deltaTime;
if (Input.GetKeyDown(KeyCode.Space))
{
verticalvelocity = jumpforce;
isgrounded = false;
}
}
else
{
verticalvelocity -= gravity * Time.deltaTime;
}
Vector2 movejump = new Vector2(rb.velocity.x, verticalvelocity);
rb.velocity = movejump;
}
}
u should check if your rb.velocity.y it’s negative , if it is then gravity is high , if it’s not then gravity is low c:
I think its the square root of (-2 * wantedJumpHeight * g), but i may be off by a bit, ill double chexk my code when im home if this is incorrect(or someone can correct me).
You could also make the player kinematic and lerp its height if it fits your controller.
I just realized that the mass is not in the equation, i think the case is multiplying the value given by the square root by the mass.
It’s possible to invent jumping rules that don’t depend on gravity, but that implies that there are at least some times during the jump that the jumper is not experiencing normal gravitational acceleration.
A more typical way of making a jump less “floaty” would be to increase gravity and also increase the initial force of the jump to compensate.
In principle, you could calculate the appropriate jumping force to reach a given height based on the amount of gravity, but it’s probably easier just to try different amounts of jumping force experimentally until you find one that looks good to you.
I’ll give that a shot, thanks for the suggestion. i just prefer working with small numbers, so i was looking if there was a different solution before i tried that
Alright, so ive been trying what @Antistone suggested and it works quite well, but there’s one last problem. Too much gravity seems to slow down horizontal movement. Is there some sort of crossover between the X and Y velocities that would cause this? can i make x and y work independently of each other?
To make my previous post more concise, if you want to specify a height and add an upwards force to get to said height with a character of particular weight in a particular g environment you can do it like so:
this function should give you the needed force:
public float JumpForceThingyCalculator(float wantedHeight, float weight, float g){
return weight * Mathf.Sqrt( -2 * wantedHeight * g);
}
to make a character jump to 10m something like this:
public float wantedJumpHeight;
bool jump = false;
Rigidbody rb;
void Start(){
rb = GetComponent<Rigidbody>();
}
void Update(){
if(Input.GetKeyDown(KeyCode.Space))
jump = true;
}
void FixedUpdate(){
if(jump){
rb.AddForce(Vector3.Up * JumpForceThingyCalculator( wantedJumpHeight, rb.mass, -Physics.Gravity.y), ForceMode.Impulse);
jump = false;
}
}
*untested
1 Like
@SparrowsNest thanks for the snippet.
It doesn’t work AS IS for me as it produces a NaN from sqrt of negative number.
Simply remove the negative sign of the -Physics.Gravity.y
did the trick.
Cheers !