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 /
avatar image
0
Question by Lukamyte · Apr 17, 2014 at 07:33 PM · gravityjumpexternal controller

Jump Unpredictable

Hello, I have a script, that controls both the gravity and the jumping. I am not using anything like character controller or anything, and it is a simple

 function Jump () {
     if(down){
                     rigidbody.AddForce(transform.up * 800 * 10 * 8 * Time.deltaTime);
     }else{
                 rigidbody.AddForce(transform.up * -800 * 10 * 8 * Time.deltaTime);
     }
 }
 
 function Gravity () {
     if(down){
         rigidbody.AddRelativeForce(transform.up * -10 * 10 * 8 * Time.deltaTime);
     }else{
         rigidbody.AddRelativeForce(transform.up * 10 * 10 * 8 * Time.deltaTime);
     }
 }

The problem is that the jump will sometimes be super short or really high. I normally would have simply used rigidbody for gravity but the game focuses on flipping gravity.

Comment
Add comment · Show 2
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 Lukamyte · Apr 17, 2014 at 07:46 PM 0
Share

Fixed Update for gravity, OnGUI for Jump

avatar image Lukamyte · Apr 17, 2014 at 09:02 PM 0
Share

bumping up

1 Reply

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

Answer by Amon · Apr 17, 2014 at 09:55 PM

 #pragma strict
 
 @HideInInspector
 var Gravity : float;
 @HideInInspector
 var JumpHeight : float;
 @HideInInspector
 var CanJump : int;
 @HideInInspector
 var falling : int;
 @HideInInspector
 var IsJumping : int;
 @HideInInspector
 var DoJump : int;
 
 
 function Start () {
     
     CanJump = 1;
     Gravity = 3.0;
     JumpHeight = 1.0;
     falling = 0;
     IsJumping = 0;
     DoJump = 0;
     
 }
 
 function Update () {
 
 }
 
 function FixedUpdate () {
     ActivateJump();
 }
 
 function ActivateJump() {
     if ( Input.GetKey(KeyCode.Space) && CanJump == 1 ) {
         if ( IsJumping == 0 ){
             IsJumping = 1;
             CanJump = 0;
             DoJump = 1;
         }
     }
     
     if ( DoJump == 1 ) {
         Jump();
     }
 }
 
 function Jump() {
     transform.position.y += JumpHeight;
     JumpHeight = JumpHeight - Gravity * Time.deltaTime;
     
     if ( JumpHeight < 0 ) {
         falling = 1;
         //do Collision checks when falling;
         //when you hit floor or platform reset everything
         FloorCollisionCheck();        
     }
 }
 
 
 function FloorCollisionCheck() {
     //just s imple function to test if the y location is below 0.
     // If it is then we stop and reset jumping;
     
     if ( transform.position.y <= 0 && falling == 1) {
         falling = 0;
         CanJump = 1;
         JumpHeight = 1.0;
         IsJumping = 0;
         DoJump = 0;
     }
 }


Don't use too high a value for JumpHeight or too low a value for Gravity when multiplying by Time.DeltaTime.

Yes it doesn't use built-in physics stuff in unity but it should put you on the correct track.

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 Lukamyte · Apr 17, 2014 at 11:20 PM 0
Share

Will this work with colliders?

avatar image Amon · Apr 17, 2014 at 11:29 PM 0
Share

Yes! Do the necessary checks in the FloorCollisionsCheck function. Or set up a new Function called, for example: ColliderCheck();.

Let us know if you have any problems. If you need an example which works with colliders I can produce one but not till tomorrow as I'm trying to get a project ready for release.

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

22 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

Related Questions

Why does my characterController code not apply gravity correctly? 0 Answers

Apply gravity to my character through script. 0 Answers

How to check if your character is falling 2D - C# 1 Answer

My jump script doesn't work every time 3 Answers

gravity doesnt seem to effect my first person controller....help! 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