Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
avatar image
1
Question by JustinTheSwift · Feb 07, 2017 at 01:45 PM · velocitygravityjumpingforce

Gravity trouble: Falling slow, jumping fast

I'm a noob so go easy on me.

I'm trying to make a simple jump function. Unfortunately, I go up very quickly and come down very slowly. Code is this:

 void FixedUpdate()
 {
     float moveHorizontal = Input.GetAxis("Horizontal");
     float moveVertical = Input.GetAxis("Vertical");
     Vector3 movement = new Vector3(moveHorizontal, 0.0F, moveVertical);

 //Note I'm not adding anything to the y vector here ^

  rb.velocity = movement * speed;

     if (Input.GetKey("space") && detectGround())
     {
         rb.AddForce(0.0F, 1000, 0.0F);

 //It seems like I'm adding way too much force on y vector, but even with that much I barely leave the ground. I need, like, 10,000 to get a good high jump.
   
     }
 }

 bool detectGround()
 {
     Vector3 down = transform.TransformDirection(Vector3.down);
     return Physics.Raycast(transform.position, down, .51F);
 }

I'm aware that I might run into weird gravity problems if I'm using odd scales, but I'm dealing with a 1x1x1 sphere in this case. Drag is also not the problem (and if it was, why do I go up so fast?). I've also tried adding some weight to my gravity by changing my y vector. That makes me fall faster but doesn't help the jump any.

Interestingly, I don't seem to have this problem if I use rb.AddForce(movement*speed) instead of rb.velocity = movement*speed. Then, however, I don't get the tight control that I want for my X axis and Z axis. (I accelerate too slowly, never hit a peak speed, and have trouble stopping).

How do I fix this?

Comment
Add comment · Show 1
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 stevehealy · Feb 07, 2017 at 02:26 PM 0
Share

if your new i would suggest a movement tutorial this one helped me the best its 12 episodes vary good vary clear explains a lot

https://www.youtube.com/watch?v=$$anonymous$$bW$$anonymous$$8bCAU2w

sorry i can't figure out how to solve your problem myself

1 Reply

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

Answer by NoseKills · Feb 07, 2017 at 04:38 PM

The problem should be here

 Vector3 movement = new Vector3(moveHorizontal, 0.0F, moveVertical);
 rb.velocity = movement * speed;

Adding a force to a rigidbody changes its velocity but that velocity only applies for 1 FixedUpdate because you specifically set the velocity.y component to 0 every FixedUpdate. Adding a huge force works because that causes velocity to change a lot so your character ends up high in the air in one FixedUpdate before you override the velocity again.

The solution is not to change the velocity.y.

 Vector3 movement = new Vector3(moveHorizontal, 0.0F, moveVertical)  * speed;
 movement.y = rb.velocity.y;
 rb.velocity = movement;


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 smpkr · Feb 01, 2021 at 10:12 PM 0
Share

thanks this solved my issue! To any that follow make sure rb.velocity=movement comes after movement.y = rb.velocity.y.

avatar image Kristiyan_19 · Feb 08 at 10:23 AM 0
Share

what if I am not using velocity but AddForce instead ?

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

8 People are following this question.

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

Related Questions

Gravitational Object Creation for 3D Game 1 Answer

Impulse to reach certain height? 1 Answer

Anti-Gravity isn't working 1 Answer

Trying to limit air velocity 2 Answers

Keeping character's movement consistent. 1 Answer


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