- Home /
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.
Its too big to read. Did you check if your rigidbody is not getting force simultaneously in every frame??
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;
}
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?
Didn't test code. It's velocity with a small v. Fixed above.
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.
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.
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!
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.
Your answer
Follow this Question
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