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 /
This question was closed Feb 02, 2015 at 07:51 PM by mediajolt for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by mediajolt · Feb 01, 2015 at 11:00 PM · physicsthird-persondouble jump

Double-jump mechanic has an initially high jump

I have a student working on a double-jump mechanic on a 3rd-person perspective. It's a simple script, but with one bug we can't figure out. The double-jump mechanic works fine after the first jump, but that first jump is really high. What is off with our logic that makes that first jump go too far? Any feedback would be most welcome!

 using UnityEngine;
 using System.Collections;
 
 public class jump : MonoBehaviour {
 
     public float jumpHeight = 0.5F;
     bool moveV;
     bool isGrounded;
     bool doubleJump;
 
     void Start ()
     {
         isGrounded = true;
         doubleJump = false;
     }
     
     void FixedUpdate ()
     {
         moveV = Input.GetButtonDown("Jump");
         if (moveV && isGrounded && !doubleJump)
         {
             rigidbody.AddForce(Vector3.up * jumpHeight);
             isGrounded = false;
             doubleJump = true;
         } 
         else if (moveV && doubleJump && !isGrounded)
         {
             rigidbody.AddForce(Vector3.up * jumpHeight);
             doubleJump = false;
         }
     }
 
     void OnCollisionEnter(Collision col)
     {
         if (col.gameObject.tag == "ground")
         {
             isGrounded = true;
             doubleJump = false;
         }
     }
 }
 
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 SnStarr · Feb 01, 2015 at 11:05 PM 0
Share

Try making his initial jump velocity as Vector3.up * jumpHeight * Time.deltaTime.

I was thinking maybe its not too high just seems that way cause of the frame skip. Try to slow it down to be frame rate independent.

1 Reply

  • Sort: 
avatar image
0

Answer by Alex_May · Feb 01, 2015 at 11:27 PM

I think what's happening is that Input is being updated in frame time, so you're getting double jump code running on the first jump. So Input.GetButtonDown("Jump") is true for more than one consecutive FixedUpdate(). You could debounce the jump button, perhaps by testing it in Update(), setting an internal bool, and then unsetting that bool when you process it in FixedUpdate().

Comment
Add comment · Show 5 · 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 mediajolt · Feb 02, 2015 at 04:37 AM 0
Share

Both solutions solved the problem of the first jump, but now the double jump isn't working. $$anonymous$$aybe I've set the boolean in the wrong place? BTW, thank both of you for the quick responses :-)

 void Update()
     {
         moveV = Input.GetButtonDown("Jump");
         hasJumped = true;
 
     }
 
     void FixedUpdate ()
     {
         if (hasJumped)
         {
             if (moveV && isGrounded && !doubleJump)
             {
                 rigidbody.AddForce(Vector3.up * jumpHeight);
                 isGrounded = false;
                 doubleJump = true;
                 hasJumped = false;
             } 
             else if (moveV && doubleJump && !isGrounded)
             {
                 rigidbody.AddForce(Vector3.up * jumpHeight * Time.deltaTime);
                 doubleJump = false;
             }
         }
     }
avatar image Alex_May · Feb 02, 2015 at 11:16 AM 1
Share

lightning quick edit, can't guarantee this will work.

  void Update()
      {
          
          hasJumped = Input.GetButtonDown("Jump");
  
      }
  
      void FixedUpdate ()
      {
          if (hasJumped)
          {
              hasJumped = false; // debounce the jump button so the next fixed update won't think we've jumped
              if (isGrounded)
              {
                  rigidbody.AddForce(Vector3.up * jumpHeight);
                  isGrounded = false;
                  doubleJump = true;
              } 
              else if (doubleJump)
              {
                  rigidbody.AddForce(Vector3.up * jumpHeight);
                  doubleJump = false;
              }
          }
 // elsewhere...
 // if on the ground
 // then set isGrounded = true and doubleJump = false
 // /elsewhere
      }
avatar image mediajolt · Feb 02, 2015 at 07:50 PM 0
Share

So simple, and yet it worked! Thank you, sir!

Interesting to see the relationship between Update and FixedUpdate and the results when things go wrong.

avatar image Alex_May · Feb 04, 2015 at 12:34 PM 1
Share

you're welcome :) please consider marking the answer as accepted!

avatar image mediajolt · Feb 06, 2015 at 04:19 PM 0
Share

I marked it as closed. Is that the same thing?

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

Apply physics material to a third person character? 0 Answers

How to make correct double jump with collor changing? 0 Answers

2D 360 degress platformer example needed 0 Answers

Double Jump Physics 1 Answer

[Physics]Forklift Problem. 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