Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ado112 · Aug 03, 2016 at 06:15 PM · scripting problemphysicsvelocitymathsformula

Setting time of the jump

I have this simple script that makes player jump when he starts pressing space.

For now player can jump for a certain height, the problem is as height gets longer, jump time also gets longer and that's not what I want, I want to make that time constant, no matter what height of jump is and no matter how strong gravity force is.

There is a trick to decrease jump time by increasing gravity when player has jumped, but that makes more problems then solutions, jump height will be shorter and I don't know how much should I increase gravity force to make the time of the jump to be a value determined by jumpTime.

Here's the code (part of it):

 using UnityEngine;
 using System.Collections;
 
 public class Player : MonoBehaviour {
 
 public float speed      = 1.0f;
 public float jumpHeight = 1.5f;
 public float jumpTime   = 1f;
 public float gravity    = 10f;
 
     Vector3 InputAxis = Vector3.zero;
     Rigidbody rig;
 
     //First frame
     void Start() {
         
         //Init rig
         rig = GetComponent<Rigidbody>();
 
         //Disable Physics.gravity on this object
         rig.useGravity = false;
 
     }
 
     //void Update() {...} //sets the inputAxis value
 
     //Physics frame
     void FixedUpdate() {
 
         //Initialize velocity change vector
         Vector3 velocityChange = Vector3.zero;
 
         //Calculate jump velocity
         //InputAxis.y is only 1.0 when space is pressed down (GetKeyDown), otherwise it's 0.0
         velocityChange.y = Mathf.Sqrt(InputAxis.y * jumpHeight * 2 * gravity);
 
         //Add velocity change
         rig.AddForce(velocityChange, ForceMode.VelocityChange);
 
         //Add gravity force
         if(velocityChange.y > 0)  //If player has jumped
             rig.AddForce(0, -gravity * jumpTime, 0, ForceMode.Acceleration); //not working...
         else
             rig.AddForce(0, -gravity, 0, ForceMode.Acceleration); 
 
         //Reset input axis
         InputAxis.Set(0, 0, 0);
 
     }
 }







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

1 Reply

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

Answer by ado112 · Aug 06, 2016 at 05:12 PM

After spending some time on wiki reading about uniformly accelerated motion, I found this nice formula for calculating gravity: a = (2 * s) / ( t ^ 2)

So in my case: a is gravity, s is jump height and t is jump time divided by 2.

By calculating velocity (v = sqrt(2 a s)) and passing it to addForce (ForceMode.VelocityChange) every time the space key is pressed down.

Using that I get quite precise results ( +- around 5% ) for jump time.

It could be more precise if I decrease fixed timestep ( bad idea ) and class that I created for counting time is totally dependent on Time.timeSinceLevelLoad (float) so perhaps that floating point precision may be a little issue in some cases.

And its important to note that the gravity calculated by this formula must be in that last AddForce (ForceMode.Acceleration) function.

Comment
Add comment · 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

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

87 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 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 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

Add velocity relative to ground? 1 Answer

When I try to save the velocity of a rigidbody and use it later it doesn't work? 1 Answer

Need help with my movement script.. :( 1 Answer

trouble making a fast projectile 2 Answers

Why are these object passing through each other? 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