How can I make my player jump faster but not higher?
Is there a way I can make my player's rigidbody jump faster but at the same height as before?
Here is the code I'm using for my player:
if(onGround)
{
rigidbody.velocity += Vector3.up * power;
}
This works just fine and the player jumps properly, but I just want to make it go faster. I wanna make my player go high faster, but still at the same height as before, since increasing the power increases the height as well, and I do not want that.
Thanks! :)
Answer by Owen-Reynolds · Dec 18, 2014 at 09:04 PM
The trick is to increase the gravity. As you saw, if you jump "harder," you go up higher. Gravity is what slows you down, and brings you back to earth. So more gravity cancels more force. On the falling side of the jump, it works the same way, to pull you down faster.
If nothing else in the scene uses gravity, you can just increase it in the Edit-Physics settings.
But, you probably have some other gravity-using rigid-bodies, and like the gravity they have now. So, you can add extra fake gravity to the player. Something like:
if(inAir) { // most code has something like this already
Vector3 vel = rigidbody.velocity;
vel.y-=BONUS_GRAV*Time.deltaTime;
rigidbody.velocity=vel;
...
}
where 9.8 is "normal" gravity (so BONUS_GRAV of 9.8 would be doube-speed.) This is a little odd, since the player gets normal gravity, plus the bonus you add. Some people might prefer to turn-off player gravity during a jump, so the fake gravity you add is the exact falling speed (but just adding is simpler to start with.)
I think it's linear, so if you double the force, and double the gravity, they "cancel" and you get the same height. Then you can do fancy tweaks, like putting it in FixedUpdate might be better for funny/variable framerates.
Could you not increase the gravity scale on that particular rigid body? Or does it max out at 1 or something?
RHZ: for 2D that would probably work (I've never used or looked at any of the 2D stuff.) But 3D rigidbodies don't have a gravityScale.
Seems it could be added, but it would slow down all physics just a little (even if you never changed it from 1.)
Up to you, but my recommendation is against the idea of changing gravity every time you jump. If you have other falling objects in your scene, or AI that can jump, it will affect them too when you would rather not. I$$anonymous$$O you are better off applying a downward force against your character ins$$anonymous$$d. calculate your current gravity with a multiplier or something to do this.
Looks like you read only the first paragraph. I'm using the writing style where you walk your way into the solution: need to increase gravity, the easy way won't work, so we have to use code to do it. That's all the way down in my third paragraph.
Oh, no, I read your post entirely. I just think the whole thing, even the solution for the gravity is a solution for a problem that doesn't need to exist to begin with. Albeit I wouldn't argue it that strongly, your solution works nonetheless.
Answer by Thorny2000 · Dec 18, 2014 at 10:00 PM
Using gravity you are limited to the physics engines parameters acting "realistically" on all the objects in the scene, realistic is nice but as you are discovering sometimes you want to break the rules for better gameplay mechanics.
Try adding this to your players script:-
float JumpVelocity,
float JumpDampening=0.1f;
void Jump()
{
JumpVelocity = 0.5f;
gameObject.rigidbody.useGravity = false;
}
void FixedUpdate()
{
Vector3 pos = transform.position;
if (JumpVelocity != 0)
{
pos.y += JumpVelocity;
JumpVelocity -= JumpDampening;
if (JumpVelocity <= 0)
{
gameObject.rigidbody.useGravity = true;
JumpVelocity = 0;
}
}
transform.position = pos;
}
In the above gravity on the object is off when jumping up, turned back on at the peak of the jump arc so gravity takes over again to bring the GO down. Play with higher start values in JumVelocity for quicker jumps, higher values in JumpDampening to slow the jump down quicker or lower values to let the jump get higher and have a more noticeable arc.
Back in the day when consoles didn't have physics engines (or the power to run them and a game) this is how things were done (usually also using int's shifted instead of floats for processing speed).
This is really throwing in one more effect -- splitting out the rising speed and falling speeds. Building this out, you could have rise speed, hang time and hang speed (for games where you jump, and get a few seconds to fire in the air) and fall speed.
I think I'd leave gravity turned off until they land. If I go up at some hand-set speed, normal gravity for down speed is going to look odd, so I probably want to hand-set down as well.
if i try this with my code then i do not fall at all, i just sit there with jump velocity equal to zero
Answer by raxashafique · Jan 17, 2016 at 09:19 AM
@Leroterr If I were you, I would use the Mathf.Clamp function to limit the height without messing my gravity in anyway whatsoever. Using Clamping I can add as much force as I want, resulting in the effect you want. i.e. The character will go up faster but not above the clamped value.
Here's a Demo Reference:
http://docs.unity3d.com/ScriptReference/Mathf.Clamp.html https://www.youtube.com/watch?v=ZggxVn93ePI
That would make the player appear to slide against an invisible ceiling.
Clamp tends to confuse people into thinking it's more than it is. Like, if you clamp y between 1 and 10, they think the system now "knows" it has to keep y in that range (it doesn't.) In every way, Clamp is just a 1-time pair of ifs.
It's a little like saying to use += or ++. Those are nice shortcuts, but they do absolutely nothing extra that a=a+1 doesn't do. Clamp is the same way. It's merely a shortcut for using your own ifs.
but by using clamping the desired effect will be achieved without messing up the physics of everything else in the game. :D Though that invisible ceiling part is ture.. :P