- Home /
How to make a proper double jump - rigidbody
I'm trying to make my double jump work consistently every time you jump..
The problem I'm having is that because of gravity, velocity.y is greater as soon as you jump than it is right before you land, and the same goes for when the character is at its zenith
for example, if jumpSpeed is 600, when I apply
Addforce(0,jumpSpeed,0);
to the beginning of the jump, the characters velocity is say 50, so if you do the double jump very fast, the character goes way high
if you do the double jump right before the character touches the ground, its like nothing happens, because velocity.y is a negative value
I tried to add
currentYVelocity = rigidbody.velocity.y
rigidbody.Addforce(0,-currentYVelocity,0)
to Addforce right before I apply the new rigidbody.addforce(0,jumpSpeed,0), but this doesn't seem to do anything
I imagine there is some complicated maths that need to be done? or perhaps something in c# that already takes care of this?
Thanks for any help
Here is my current jump functionality to mitigate this variation in velocity, but its not really accurate... surely there is a proper way
in Update()
if((jumpCount < 2 && Input.GetButtonDown("Jump")) && !bossArea) // ******* jump
{
if(jumpCount == 0)
{
inputA = Time.time;
Jump ();
audio.PlayOneShot(jump_a);
}
if((jumpCount == 1 && Input.GetButtonDown("Jump")) && !bossArea)
inputB = Time.time;
if((inputB - inputA) > 0.15f)
{
Jump();
audio.PlayOneShot(jump_b);
}
}
and the function
void Jump()
{
if(!isInCloud) // ********* $$anonymous$$aking sure jumping is consistent
{
rigidbody.AddForce(0,-currentYVelocity,0);
isGrounded = false;
if(jumpCount == 0)
{
rigidbody.AddForce(0,jumpSpeed,0);
++jumpCount;
}
if(((inputB - inputA) > 0.15f) && ((inputB - inputA) < 0.2f))
{
rigidbody.AddForce(0,jumpSpeed - 500,0);
++jumpCount;
}
if(((inputB - inputA) > 0.2f) && ((inputB - inputA) < 0.4f))
{
rigidbody.AddForce(0,jumpSpeed - 400,0);
++jumpCount;
}
if(((inputB - inputA) > 0.4f) && ((inputB - inputA) < 0.5f))
{
rigidbody.AddForce(0,jumpSpeed + 100,0);
++jumpCount;
}
if(((inputB - inputA) > 0.5f) && ((inputB - inputA) < 0.6f))
{
rigidbody.AddForce(0,jumpSpeed + 300,0);
++jumpCount;
}
if(((inputB - inputA) > 0.6f) && ((inputB - inputA) < 0.7f))
{
rigidbody.AddForce(0,jumpSpeed + 500,0);
++jumpCount;
}
if(((inputB - inputA) > 0.7f) && ((inputB - inputA) < 0.8f))
{
rigidbody.AddForce(0,jumpSpeed + 900,0);
++jumpCount;
}
if(((inputB - inputA) > 0.8f) && ((inputB - inputA) < 1f))
{
rigidbody.AddForce(0,jumpSpeed + 1300,0);
++jumpCount;
}
if((inputB - inputA) > 1)
{
rigidbody.AddForce(0,jumpSpeed + 1500,0);
++jumpCount;
}
}
The idea is that the double jump for a side scroller should not deviate - the jump should always have the same amount of force, that way if the player needs to make a split second decision, they are not penalized because of gravity. Hope this makes sense : )
Answer by digzelot · May 20, 2014 at 06:15 AM
I simply overlooked the obvious... for anyone with a similar issue, do not use Addforce .... simply change velocity directly.
The above work less effectively than this
void Jump()
{
rigidbody.velocity = new Vector3(0, 30, 0);
jumpCount++;
isGrounded = false;
}
Your answer
Follow this Question
Related Questions
i'dont want double Jump 1 Answer
Player Jump via Transform on Input Space 2 Answers
Character Controller Jumping Higher against Box Collider 0 Answers
Jumping Issue, Need Help. 0 Answers
How Can I Make My Character Double Jump and Wall Jump? 1 Answer