- Home /
Trouble with Double Jumping Under a Constant Force
So I have a player character that is capable of double jumping, which is achieved through the rb.AddForce command. I also have a constant force placed on the character so they fall to the ground faster. the double jump code works fine if used when the player is on the way up or at the peak of their first jump. The issue is, if my character starts moving down, the double jump force adds with the much stronger constant force to effectively nullify it, which isn't good.
What I want is either a way to increase the fall speed of the character without a constant force or disable/weaken the constant force very briefly to allow the double jump to work properly before resetting it so thew player isn't so floaty. Any suggestions?
Answer by Philosophbot · Nov 14, 2019 at 01:56 PM
Well, there are a couple of things you can do. Unity engine has a built-in gravity system for the rigid-body component if you want to use that(it would probably act similar to your constant force. Although I think the best way to fix your problem would be to add more force to your jumps. Maybe you could have it so that every time the player jumps you apply the JumpForce + (the constant force at that moment) to nullify the effect of the constant force on the jump.
The issue with that is that the double jump on the upward arc would become super strong (as the applied force of the double jump combines with the force of the first jump(for example, if Jump1 force is 4 and Jump2 force is like 15, the 15 combines with the 4 to create a monstrously powerful jump) ), while on the way down to the ground it would be weaker, since it's being subtracted from by the downward constant force. I'm trying to find a way to keep the double jump consistent in power whether it's going up or down while keeping a fast and realistic falling speed.
EDIT: Where might I find this gravity option?
To fix this problem of the super jump on the upward arc, you could use an if statement that checks to see if the character is falling. The easiest way to do this would probably be to find the velocity of the Rigidbody. Unity engine has an easy way to check this in the form of Rigidbody.velocity which will return a Vector3 with the different velocities for x,y,z.
You could say If(rb.velocity.Y < 0) // do jump with cancel force
this works because if an object has a negative y velocity, you know that the object is falling.
In terms of the gravity option, the Rigidbody component has a checkbox for gravity in the editor window:
Here is the link to the Unity Rigidbody manual for more information: https://docs.unity3d.com/$$anonymous$$anual/class-Rigidbody.html
I did find the gravity option, which gave my character a much more natural fall speed, thanks. I was wondering, would there potentially be a way to set the rigidbody's vertical movement to zero, then immediately add in the double jump? That way, it is always the same regardless of previous direction of movement.
Answer by Spiritdon · Apr 16, 2021 at 09:38 PM
All you really need to do is cancel the force of acceleration
if (Input.GetKeyDown(KeyCode.Space)&& extraJumpCounter != 0)//allow the player to jump as long asthey are pressing space and they have extra jumps
{
StartCoroutine(JumpParticle()); //Play Particle effect
extraJumpCounter--;
float forceOfAcceleration =Mathf.Abs(rb.mass*Physics.gravity.y);//the force of acceleration
if (IsFalling())//canceling out the Force of Gravity for mroe effective double jumping
{
float tempForce = jumpForce + forceOfAcceleration;
rb.AddForce(new Vector3(0, tempForce, 0), ForceMode.Impulse);
}
else
{
rb.AddForce(new Vector3(0, jumpForce, 0), ForceMode.Impulse);
}
}