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 ChromaRay · Nov 08, 2013 at 03:01 AM · rigidbodyinputjumpsensitivity

Jump Input Sensitivity

(Sorry to the moderators, I deleted this post thinking I fixed it but I was mistaken)

Hello,

First i'd like to say pardon my amateurish coding, i'm still new to this. I'm trying to add sensitivity to my jump input for a 2D platformer so that the longer the jump button is held, the higher you jump.

I've been messing around with it all day, and have somewhat gotten results, however the jump is still a little unrefined and often times will not return the correct jump height (The jump button held down will sometimes result in a short jump and other times a long jump, for example). Based on my code, what might I need to change/add in order to allow a short jump for a simple press and a large jump for a constant press? Maybe there's a better method you would recommend?

My code is based on a rigidbody FPS script posted on the Unity wiki that I liked, modified for a 2D platformer. I've tried both Input.GetAxis("Jump") with low sensitivity as well as a custom float that adds while the jump button is held, capping out with a clamp, both with the same results of a random jump height.

Here's a code snippet of the better method, using the float that adds while the space bar is pressed. It changes the jump height, however it seems to be random rather than dependent on how long the jump button is held.

 using UnityEngine;
 using System.Collections;
 
 [RequireComponent (typeof (Rigidbody))]
 
 public class JackMoves : MonoBehaviour 
 {
 
 public bool letsJump = false;
 public bool grounded = false;
 public float jumpStrength = 0f;
 public float jumpStrengthAdd = 0.2f;
 public float jumpVariable;
 public float jumpHeight = 5.5f;
 public float gravity = 36.0f; 
 
 void Update()
 {
         // Jump
         if (grounded == true && Input.GetKeyDown ("space"))
         {
             letsJump = true;
         }
         
         // Not jumping
         if (grounded == false || Input.GetKeyUp ("space"))
         {
             letsJump = false;    
         }
         
         // Gets sensitivity on the Space Bar press
         if (Input.GetKey ("space"))
         {
             
             jumpStrength += jumpStrengthAdd;
             jumpStrength = Mathf.Clamp(jumpStrength, 0, 2);
             jumpVariable = Mathf.Sqrt(jumpStrength * jumpHeight * gravity);
             
         }
         
         // Resets the sensitivity
         if (Input.GetKeyUp ("space"))
         {
             
             jumpStrength = 0f;
             jumpVariable = 0f;
             
         }
 }
 
 void FixedUpdate()
 {
         // Jump
         if (letsJump == true)
         {            
             rigidbody.velocity = new Vector3(0, jumpVariable, 0);
         }
 }



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 mattssonon · Nov 08, 2013 at 11:07 AM 0
Share

jumpStrength will reach 2 VERY fast when it's incremented with 0.2 on every frame, try decreasing jumpStrengthAdd.

0 Replies

· Add your reply
  • Sort: 

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

16 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

Related Questions

Player object falling slowly when holding input 2 Answers

What's wrong with this script? 4 Answers

Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers

How do I make my character jump over a cube with rigid body moving towards it 1 Answer

ForceMode.Impulse Doesnt always fire. 2 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