• 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 /
  • Help Room /
avatar image
1
Question by FpsLukonja · Aug 24, 2013 at 12:16 PM · double jumpwall jumpdodge

Double Jump Not Working Good

My double jump script works however after a while, to be more specific after jumping around my scene, suddenly i can't double jump anymore, and only 1 jump works then. Can someone check whats wrong? I am also trying to get my FPS player to walljump, and gain jumping movement speed while jumping, which slows down after i stop jumping.

Also my air control is massive and i notice there is no gravity when i jump from boxes i jst fall down, can someone help, or point me to what am i doing wrong? Here is my movement script by following tutorials etc. I am trying to get feeling as between quake and unreal tournament genres.

    using UnityEngine;
     using System.Collections;
     
         [RequireComponent(typeof(CharacterController))]
         public class FPSController : MonoBehaviour
         {
         public float movementSpeed = 5.0f;
         public float jumpSpeed = 8.0f;
         int jumpCount = 0;
         int maxJumps = 1;
         float verticalVelocity = 0;
         
         CharacterController characterController;
         
         void Awake () {
             Screen.lockCursor = true;
             characterController = GetComponent<CharacterController>();
         }
         
          
             void Update()
             {
          
          
             float forwardSpeed = Input.GetAxis("Vertical") * movementSpeed;
             float sideSpeed = Input.GetAxis("Horizontal") * movementSpeed;
             
             verticalVelocity += Physics.gravity.y * Time.deltaTime;            
     
             
             if( characterController.isGrounded && Input.GetButtonDown("Jump") ) {
                 verticalVelocity = jumpSpeed;
                 jumpCount++;        
             }
             
             if(jumpCount==maxJumps && !characterController.isGrounded && Input.GetButtonDown("Jump") ) {
                 verticalVelocity = jumpSpeed;
                 jumpCount=0;
             }
             
             Vector3 speed = new Vector3( sideSpeed, verticalVelocity, forwardSpeed );
             
             speed = transform.rotation * speed;
             
             characterController.Move( speed * Time.deltaTime );
             
                  
         }
     }
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
2
Best Answer

Answer by -hiTo- · Aug 24, 2013 at 03:39 PM

I see a problem with your if-statements.

What if you only jump once, then you're back on the ground. Then you do it again. Then you do it again.

Your jumpCount would be 3, and your second if-statement would never run.

Set your jumpCount in your first if statement to 0, and it should solve the problem.

Just a tip: If you use a bool, you would only need 1 variable, instead of 2 integers. Example:

 private bool HasDoubleJumped = false;
 
 private void Update()
 {
     if (characterController.isGrounded && Input.GetButton("Jump"))
     {
         verticalVelocity = jumpSpeed;
         HasDoubleJumped = false;
     }
     else if (!HasDoubleJumped && !characterController.isGrounded && Input.GetButton("Jump"))
     {
         verticalVelocity = jumpSpeed;
         HasDoubleJumped = true;
     }
 }
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 FpsLukonja · Aug 24, 2013 at 04:40 PM 0
Share

Thank you, it worked! I was thinking all day how to do this. And you solve it in few $$anonymous$$utes. Here is solution.

 if(characterController.isGrounded && Input.GetButtonDown("Jump") ) {
                 verticalVelocity = jumpSpeed;
                 jumpCount=0;        
             }
             
             if(jumpCount<maxJumps && !characterController.isGrounded && Input.GetButtonDown("Jump") ) {
                 verticalVelocity = jumpSpeed;
                 jumpCount=1;
             }
avatar image -hiTo- · Aug 24, 2013 at 05:16 PM 0
Share

Please mark the answer as correct. Thanks!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



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

2D sidescrolling game. Jumping problems through platforms and platform effector. 0 Answers

how do i add double jumping into my 2d prefab character controlle 0 Answers

Can't get my walljump to work on the new input system 0 Answers

How to make a proper double jump with my code? 0 Answers

Double Jump problem [C#] 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