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 MattTheProgrammer · Jan 01, 2014 at 05:45 AM · c#jumpdoublejump

Double Jump makes my character fly into the sky

Hi Im making a 2D Platformer and have just added a Double Jump ability to my character. The problem is that when You double tap the guiTexture(Android Touch controls) The character does the first jump fine, Then on the second Jump (Double Jump) He jumps really high, Up to the ceiling!

Here are the two scripts PlayerLogic.cs and PlayerJump.cs

PlayerLogic.cs using UnityEngine; using System.Collections;

 public class PlayerLogic : MonoBehaviour {
 
     public Transform GroundStart, GroundEnd;
     public bool grounded  = false;
     public static float jumpForce = 0f;
     public static float doubleJumpForce = 0f; 
     public static bool doubleJump = false;
     public float MoveSpeed = 3f;
     public static int jumpCount = 0; 
 
     //Movement booleans
     public static bool MoveRight = false;
     public static bool MoveLeft = false;
     public static bool Jump = false;
     // Use this for initialization
     void Update()
     {
         //Run these to functions
         Raycasting();
         Movement();
     }
 
     void Raycasting()
     {
         //Draw a line from the start position, to the end position.
         Debug.DrawLine(this.transform.position, GroundEnd.position, Color.green);
         //If Raycast detects the Ground layer from the end point of the line, then IsGrounded becomes true
         grounded = Physics2D.Linecast(this.transform.position, GroundEnd.position, 1 << LayerMask.NameToLayer("Ground"));
     }
 
     void Movement()
     {
         //Touch Controls
 
         if(MoveRight == true)
         {
             transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         if(MoveLeft == true)
         {
             transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 180);  
         }
         
         if(Jump == true && grounded == true)
         {
             rigidbody2D.AddForce(Vector2.up * jumpForce);
             Jump = false;
         }
         if(doubleJump == true && grounded == false) 
         {
             doubleJumpForce = 300f;
             rigidbody2D.AddForce(Vector2.up * doubleJumpForce);
             doubleJump = false;
         }
         if(grounded == true)
         {
             jumpCount = 0; 
         }
         if(jumpCount == 0)
         {
             jumpForce = 0f;
             doubleJumpForce = 0f;
         }
         if(jumpCount == 1)
         {
             Debug.Log("1 Jump");
         }
         else if(jumpCount == 2)
         {
             Debug.Log("2 Jumps");
         }
 
         //Key Controls
 
         /*if(Input.GetKey (KeyCode.D))
         {
             transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 0);
         }
         if(Input.GetKey (KeyCode.A)) 
         {
             transform.Translate(Vector2.right * MoveSpeed * Time.deltaTime);
             transform.eulerAngles = new Vector2(0, 180);  
         }
         
         if(Input.GetKeyDown (KeyCode.Space) && grounded)
         {
             rigidbody2D.AddForce(Vector2.up * jumpForce);
         }*/
 
     }
 }

PlayerJump.cs

 using UnityEngine;
 using System.Collections;
 
 public class PlayerJump : MonoBehaviour {
     
     // Update is called once per frame
     void Update () 
     {
         //Is there a touch?
         if(Input.touches.Length <= 0)
         {
             //If no touches...
         }
         else //If screen touched..
         {
             //Loop through these
             for(int i = 0; i < Input.touchCount; i++)
             { 
                 //executes this code for current touch
                 if(this.guiTexture.HitTest(Input.GetTouch(i).position))
                 {
                     PlayerLogic.Jump = true;
                     PlayerLogic.jumpCount = 1;
                     PlayerLogic.jumpForce = 300f;
                 }
 
                 if(Input.GetTouch(i).tapCount == 2)
                 {
                     PlayerLogic.doubleJump = true;
                     PlayerLogic.jumpCount = 2;
                     PlayerLogic.doubleJumpForce = 300f;
                 }
             }
         }
     }
 }


I hope you can understand my question.

Thanks in advance.

Comment
Add comment · Show 2
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 adnirjhar · Jan 01, 2014 at 05:55 AM 0
Share

Its too big to read. Did you check if your rigidbody is not getting force simultaneously in every frame??

avatar image MattTheProgrammer · Jan 01, 2014 at 10:02 AM 0
Share

@adnirjhar How do I check that?

4 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by HappyMoo · Jan 01, 2014 at 06:49 AM

You're adding forces. Now... adding the same force the first time to a grounded character and the second time to one in full upward motion, won't result in the same jump height.

To get a behaviour that more resembles the classic doublejump, set the velovity to zero when doublejumping.

 if(doubleJump == true && grounded == false) 
 {
     Vector3 vel = rigidbody2D.velocity;
     vel.y = 0;
     rigidbody2D.velocity = vel;
     doubleJumpForce = 300f;
     rigidbody2D.AddForce(Vector2.up * doubleJumpForce);
     doubleJump = false;
 }
Comment
Add comment · Show 3 · 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 MattTheProgrammer · Jan 01, 2014 at 07:56 AM 0
Share

Hi @Happy$$anonymous$$oo I tried your code but I got 2 Compiler errors saying 'UnityEngine.Rigidbody2D' does not contain a definition for 'Velocity' Any way to fix this or another solution?

avatar image HappyMoo · Jan 01, 2014 at 12:05 PM 0
Share

Didn't test code. It's velocity with a small v. Fixed above.

avatar image sepharg · Oct 09, 2016 at 08:47 AM 0
Share

Thanks man I was tring to avoid the scenario when pressing double jump twice fast made the player go up much higher into the sky, setting velocity to 0 on the double jump fixed it.

avatar image
0

Answer by Pkenna · Jan 01, 2014 at 06:32 AM

I'm not great with C# but it looks like you are applying a force in the same direction twice. If the force from the first jump is still acting on the rigid body when you apply the second force that may explain it. Try stopping your rigid body right before you apply the doubleJump force to it.

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
avatar image
0

Answer by Shinyclef · Jan 01, 2014 at 06:31 AM

Delete line 53 from PlayerLogic. -> doubleJumpForce = 300f;

You must have accidentally left that in and not noticed.

Edit: I noticed your are already manually setting your double jump force to 300 anyway so Pkenna and HappyMoo are correct.

Still, you might want to remove that line so you don't wonder why your double jump force is not changing later!

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
avatar image
0

Answer by JSierraAKAMC · Jan 01, 2014 at 07:33 AM

I believe the problem is that you are adding too much force. The rigidbody is not acting with the ground when you are in the air (unlike the initial jump), so you should use a smaller force with your double jump.

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

24 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 avatar image avatar image avatar image

Related Questions

Jump and move (CharacterController.velocity) C# 0 Answers

A node in a childnode? 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Ok, going to try to reach out on this issue one more time. 3 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