Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Leroterr · Dec 18, 2014 at 08:33 PM · rigidbodyvector3velocitygravityjump

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! :)

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
7
Best Answer

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.

Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image RedHedZed · Dec 18, 2014 at 09:08 PM 0
Share

Could you not increase the gravity scale on that particular rigid body? Or does it max out at 1 or something?

avatar image Owen-Reynolds · Dec 19, 2014 at 04:02 PM 0
Share

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.)

avatar image Kerihobo · Jan 17, 2016 at 04:42 AM 0
Share

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.

avatar image Owen-Reynolds Kerihobo · Jan 17, 2016 at 05:09 PM 0
Share

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.

avatar image Kerihobo Owen-Reynolds · Jan 17, 2016 at 09:16 PM 0
Share

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.

Show more comments
avatar image
3

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).

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Dec 19, 2014 at 03:54 PM 0
Share

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.

avatar image smallville7123 · Dec 02, 2018 at 01:21 AM 0
Share

if i try this with my code then i do not fall at all, i just sit there with jump velocity equal to zero

avatar image
0

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

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Owen-Reynolds · Jan 17, 2016 at 04:47 PM 0
Share

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.

avatar image raxashafique Owen-Reynolds · Jan 17, 2016 at 09:35 PM 0
Share

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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

32 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Gravity To CharacterController 0 Answers

Using rb.velocity causes low gravity. 2 Answers

Set the Velocity to reach a position at a specific Timestamp 0 Answers

Please help me figure this out 0 Answers

Slow down a character while they are midair while keeping their original velocity 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges