- Home /
Character not jumping when moving
I'm having a problem where does not jump while running. It does jump, but not every time i hit the up arrow. When I'm standing still however, the character jumps every time i hit the up arrow key. Even when I remove the ground check, the character will jump while running about 40% of the time.
my jump method:
public void Jump()
{
if (controller.IsGrounded)
{
// Set our new Velocity to jump to our predesignated height
controller.Velocity = new Vector2(rigidBody.velocity.x, Mathf.Sqrt(-2.0f * JumpHeight * Physics2D.gravity.y));
}
}
my Update() method:
var horizontalInput = Input.GetAxis("Horizontal");
// player movement
if (Mathf.Abs(horizontalInput) > 0.0f || moveRight || moveLeft)
{
if (horizontalInput > 0.0f || moveRight == true)
{
if(moveRight == true)
horizontalInput = 1;
if (controller.IsGrounded || wasRunningBeforeJump)
{
wasRunningBeforeJump = true;
controller.Move(horizontalInput);
}
else
controller.Move(horizontalInput * .5f);
facingRight = true;
}
if (horizontalInput < 0.0f || moveLeft == true)
{
if(moveLeft == true)
horizontalInput = -1;
if (controller.IsGrounded || wasRunningBeforeJump)
{
wasRunningBeforeJump = true;
controller.Move(horizontalInput);
}
else
controller.Move(horizontalInput * .5f);
facingRight = false;
}
if (controller.IsGrounded)
RunAnimation();
}
//idle
if (controller.Velocity == Vector2.zero && !touchScreenMode)
{
IdleAnimation();
wasRunningBeforeJump = false;
}
if(!moveLeft && !moveRight && touchScreenMode && controller.IsGrounded)
{
rb.velocity = Vector2.zero;
IdleAnimation();
}
// jump
if (Input.GetKeyDown(KeyCode.UpArrow) || jumping)
{
if (wasRunningBeforeJump) {
jumper.Jump();
}
else if (!wasRunningBeforeJump)
{
IdleAnimation();
jumper.Jump();
}
}
the moveLeft and moveRight are for event triggers.
Edit: character jumps properly when I hit the UI event trigger to jump. does not jump properly using up arrow
How thoroughly did you debug? A first step would be to put Debug.Log() calls inside of each IF to see which expression is blocking the jump.
I suspect that either runAnimation or wasRunningBeforeJump is blocking it.
You probably need to also post the code that is setting those values.
I haven't really debugged all of the if statements, but i've put Debug.Log() calls inside of both if statements in the (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.UpArrow) method. everytime i tried to jump with the arrow key, it would "jump" once. However, I noticed when I clicked on the UI event trigger, it would log "jump" about 18+ times.
So if you did that, you should know what is blocking him to jump.
The current code (did you edit it?) shows that the jump() method is called whenever you start pressing the up arrow. The only thing preventing it from setting the velocity then is the IsGrounded.
I still don't have enough information to go further. $$anonymous$$g. you could edit your code to add different printouts to all the necessary places and post the printout in the case the jumping fails.
Also "not jumping" means what exactly? The line of code setting the velocity is not reached? The character is just not moving vertically? Probably some other code reacts on the cursor key as well and modifies the velocity again. Check for other places modifying the velocity.
Your answer