- Home /
My character double jumps if he runs, before jumping??
I want my character to just jump once, and it works as long as he doesn't run. If just stand there and jump, he can only do it once (until he collides with the ground again), but if he runs, and then jumps he can make an extra jump before unable to jump again. I have NO idea why. Can anyone here help?
Here's the part of the code:
void Update () {
if(Input.GetKey(KeyCode.RightArrow)) {
transform.Translate(Vector2.right * Time.deltaTime*speed);
transform.localScale = new Vector3(1, 1, 1);
}
if (Input.GetKey(KeyCode.LeftArrow))
{
transform.Translate(-Vector2.right * Time.deltaTime * speed);
transform.localScale = new Vector3(-1, 1, 1);
}
if (isTouchingGround==true)
{
if (Input.GetKeyDown(KeyCode.UpArrow))
{
rigidbody2D.AddForce(Vector2.up * jumpForce);
isTouchingGround = false;
}
}
void OnCollisionEnter2D(Collision2D coll)
{
if (coll.gameObject.tag == "Ground")
isTouchingGround = true;
}
Put a Debug.Log() into OnCollisionEnter2D(). Also add a OnCollisionExit2D() and put a Debug.Log() in that callback. This will give you more information about what is going on.
Ok so made a Debug.Log in OnCollisionEnter2D and made a OnCollisionExit2D and made a Debug.Log for that too. Still doesn't work. Only works when player stands still. When he runs on the ground it spams the Debug.Log in OnCollisionEnter2D, and if he jumps while running it doesn't send the Debug.Log from OnCollisionExit2D. I have no idea of what to do.
void OnCollisionEnter2D(Collision2D coll) {
if (coll.gameObject.tag == "Ground")
Debug.Log("is touching ground");
isTouchingGround = true;
}
void OnCollisionExit2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
Debug.Log("is not touching ground");
isTouchingGround = false;
}
Answer by uanmanarmy · Apr 15, 2014 at 10:38 PM
You can watch this live training. And refactor your code.
http://unity3d.com/learn/tutorials/modules/beginner/2d/2d-controllers
Your answer
![](https://koobas.hobune.stream/wayback/20220613142800im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
jump on collision weird behaviour 2 Answers
jump on collision - first person controller 0 Answers
Jumping on top of an enemy problem 1 Answer