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 Aggrojag · Apr 13, 2014 at 11:17 PM · playerjumpjumpingcontrolsgetkeydown

Jump Further When Key Is Held Down

I'm trying to make my player jump further if they hold down the W key. However, it appears that my player will always jump the same distance. I've been trying to figure this out for a rather long period of time now, and have not been able to find an answer yet. Thank you much for any help!

 using UnityEngine;
 using System.Collections;
 
 public class JumpInC : MonoBehaviour {
 
     private float distToGround;
 
     //Height of jump
     public float jumpHeight;
 
     //To allow fo slopes
     public float skin;
 
     //How fast player is forced down.
     public float jumpDown;
 
     public bool jumping = false;
     
     void Start(){
         // get the distance to ground
         distToGround = collider.bounds.extents.y;
     }
 
 
     //Is the player grounded?
     bool IsGrounded() {
         return Physics.Raycast(transform.position, -Vector3.up, distToGround + skin);
     }
     
     void FixedUpdate () {
 
         //Jump
         if (Input.GetKey(KeyCode.W) && IsGrounded()){
             rigidbody.AddForce(new Vector3(0, jumpHeight , 0),ForceMode.Impulse);
             jumping = true;
 
         }
 
         //Jump Further if Held Down?
         if (Input.GetKey(KeyCode.W) && jumping == true){
             rigidbody.AddForce(new Vector3(0, 1.5f * jumpHeight , 0),ForceMode.Impulse);
             
         }
 
         //If W is released, jump shorter distance
         if (Input.GetKeyUp (KeyCode.W) && jumping == true || IsGrounded ()) {
             jumping = false;        
         }
 
         //Push player downwards
         if (Input.GetKey (KeyCode.S) && IsGrounded() == false) {
             rigidbody.AddForce(0, -jumpHeight * jumpDown, 0);
             jumping = false;
         }
     }
 }
Comment
Add comment · Show 5
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 Meltdown · Apr 13, 2014 at 11:23 PM 0
Share

Have you tried using Debug.Log to Debug each if statement to see what is actually happening? So in each if Debug.Log the value of jumping, and also use Debug.Log to check if the if statement is actually being hit.

avatar image Aggrojag · Apr 13, 2014 at 11:37 PM 0
Share

I really need to get in the habit of using Debug.Log... It was the || IsGrounded() on line 46... However, my object is now overco$$anonymous$$g gravity unless my values are quite low. Anyways, the original question posed is solved. Thanks $$anonymous$$eltdown : ).

avatar image robertbu · Apr 13, 2014 at 11:44 PM 0
Share

@PainoHandsThePerson - I'd just come back to post the answer when I noticed you'd figure it out. You should explain your solution a bit more as an answer and then mark your question as answered.

avatar image Aggrojag · Apr 13, 2014 at 11:52 PM 0
Share

Did so just now. I feel odd accepting my answer as correct for my question when it was due to the input of another person in the community >_<.

avatar image robertbu · Apr 14, 2014 at 12:55 AM 0
Share

@PianoHandsThePerson - it can be a fine line. I'd say the comment headed you down a successful path, but was not the answer. When you get a bit more $$anonymous$$arma, giving them a thumbs up is the right thing to do.

1 Reply

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

Answer by Aggrojag · Apr 14, 2014 at 12:53 AM

The object can now overcome gravity, however the question is resolved. it was due to || is Grounded() on line 46.

 using UnityEngine;
     using System.Collections;
      
     public class JumpInC : MonoBehaviour {
      
     private float distToGround;
      
     //Height of jump
     public float jumpHeight;
      
     //To allow fo slopes
     public float skin;
      
     //How fast player is forced down.
     public float jumpDown;
      
     public bool jumping = false;
      
     void Start(){
     // get the distance to ground
     distToGround = collider.bounds.extents.y;
     }
      
      
     //Is the player grounded?
     bool IsGrounded() {
     return Physics.Raycast(transform.position, -Vector3.up, distToGround + skin);
     }
      
     void FixedUpdate () {
      
     //Jump
     if (Input.GetKey(KeyCode.W) && IsGrounded()){
     rigidbody.AddForce(new Vector3(0, jumpHeight , 0),ForceMode.Impulse);
     jumping = true;
      
     }
      
     //Jump Further if Held Down?
     if (Input.GetKey(KeyCode.W) && jumping == true){
     rigidbody.AddForce(new Vector3(0, 1.5f * jumpHeight , 0),ForceMode.Impulse);
      
     }
      
     //If W is released, jump shorter distance
     if (Input.GetKeyUp (KeyCode.W) && jumping == true) {
     jumping = false;
     }
      
     //Push player downwards
     if (Input.GetKey (KeyCode.S) && IsGrounded() == false) {
     rigidbody.AddForce(0, -jumpHeight * jumpDown, 0);
     jumping = false;
     }
     }
     }
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

21 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

Related Questions

Jumping not allways registering fix? 0 Answers

Problem between my player jump and player movement 1 Answer

How to make the player turn in the air 1 Answer

Jumping while moving 1 Answer

Why does my character never stop jumping? 0 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