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
1
Question by konashadow · Jun 19, 2013 at 05:41 AM · jump

Hold space longer = Longer jumps?

Hey all,

I'm trying to make a game where the jump length is changed based on if the player is holding the space bar or just tapping it.

I honestly don't have an idea of where to start with this, so all help is appreciated.

Thanks

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 iwaldrop · Jun 19, 2013 at 06:15 AM 0
Share

You could add force while the button is down up to a certain duration. Once the duration is surpassed just don't add the force. Reset the button down time whenever the character is on the ground and the space button is depressed.

2 Replies

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

Answer by fafase · Jun 19, 2013 at 09:47 AM

Let's consider you are doing a 2D platform game. If not, no worry, the principle remains the same.

Her is a class that allows you to do some movement in 2D and the jump has a minimal jump and if you keep on pressing the jump is larger:

 public float speed = 6.0F;
 public float jumpSpeed = 20.0F;
 public float gravity = 20.0F;
 public float gravityForce = 3.0f; 
 public float airTime = 2f;
 private Vector3 moveDirection = Vector3.zero;
 private CharacterController controller;
 private float forceY = 0;
 private float invertGrav;
 
 void Start(){
     // invertGrav is set greater than gravity so that our guy jumps
     invertGrav = gravity * airTime;
     controller = GetComponent<CharacterController>();
 }
 void Update() {   
     moveDirection = new Vector3(Input.GetAxis("Horizontal"),0,0);
     moveDirection = transform.TransformDirection(moveDirection);
     moveDirection *= speed;    
     if (controller.isGrounded) {
         // we are grounded so forceY is 0
         forceY = 0;
         // invertGrav is also reset based on the gravity
         invertGrav = gravity * airTime;
         if (Input.GetKeyDown(KeyCode.Space)){
             // we jump 
             forceY = jumpSpeed;
         }
     }
     // We are now jumping since forceY is not 0
     // we add invertGrav to our jumpForce and invertGrav is also
     // decreased so that we get a curvy jump
     if(Input.GetKey(KeyCode.Space) && forceY != 0 ){
         invertGrav -= Time.deltaTime;
         forceY += invertGrav*Time.deltaTime;
     } 
     // Here we apply the gravity
     forceY -= gravity*Time.deltaTime* gravityForce;
     moveDirection.y = forceY;
     controller.Move(moveDirection * Time.deltaTime);
 }

It might not be the best and it probably needs some modifications but it works...

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 konashadow · Jun 19, 2013 at 07:18 PM 0
Share

Works great, thanks a lot. I just separated the jumping from the body of the script and added it to my current move script.

avatar image Javyer · Sep 21, 2015 at 05:01 PM 0
Share

The problem I found with this is that On$$anonymous$$eyUp the player falls abruptly but if he keep the key pressed he falls slowly.

avatar image
3

Answer by elhispano · Jun 19, 2013 at 06:50 AM

I recommend you to work with AddForce. The first time you press space, apply an instant force with ForceMode.VelocityChange, this will be the basic jump, and then, while Space button is pressed, continue using AddForce but with ForceMode.Acceleration as parameter so each frame Space key keep pressed, yo apply a force to your character so it will continue going up before start falling.

 void Jump ()
 {
     if (Input.GetKeyDown (KeyCode.Space)) {
         rigidbody.AddForce(Vector3.up * 10,ForceMode.VelocityChange);
     }
  
     if (Input.GetKey (KeyCode.Space)) {
        if (jumpValue < maxJumpValue) {
          rigidbody.AddForce(Vector3.up * 10,ForceMode.Acceleration);
        }
  
     }
  
 }


Comment
Add comment · Show 1 · 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 Raunchard · Nov 21, 2015 at 12:16 AM 1
Share

good Idea, but you shouldn't use addforce in the update cycle, because its not framerate independent. to solve that quick and dirty multiply the force with time.deltatime to take the changing framerate into account.

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

20 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

Related Questions

Help Me with My Script Pls?????? 1 Answer

How difficult would it be? 1 Answer

Animation Cycle Dilemma 0 Answers

Tilt control for jump 0 Answers

How to make smooth jump? 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