2D Character won't jump. No Errors.

I can’t figure this out. My character just won’t jump. I’m not getting any errors.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerMovement : MonoBehaviour
{
public float movepseed;
public float jumpForce;
private Rigidbody2D rb;
private bool facingRight = true;
private float moveDirection;
private bool isJumping = false;
// Awake is called after all objects are initiated. Called in render order
private void Awake()
{
rb = GetComponent(); //will look for a component on this GameObject (what is attached to to) of type rigidbody2D
}

// Update is called once per frame
void Update()
{
// get player input
ProcessInputs();
//animate
Animate();
}
private void FixedUpdate()
{
//move
Move();
}
private void Move()
{
rb.velocity = new Vector2(moveDirection * movepseed, rb.velocity.y);
if(isJumping)
{
rb.AddForce(new Vector2(0f, jumpForce));
}
isJumping = false;
}
private void Animate()
{
if (moveDirection > 0 && !facingRight)
{
flipCharacter();
}
else if (moveDirection < 0 && facingRight)
{
flipCharacter();
}
}
private void ProcessInputs()
{
moveDirection = Input.GetAxis(“Horizontal”);
if(Input.GetButtonDown(“Jump”))
{
isJumping = true;
}
}
private void flipCharacter()
{
facingRight = !facingRight; //inversion bool
transform.Rotate(0f, 180f, 0f);
}
}

If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: Using code tags properly

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run?
  • what are the values of the variables involved? Are they initialized?

Knowing this information will help you reason about the behavior you are seeing.

If you are running a mobile device you can also see the console output. Google for how.

1 Like

Your code works for me.

After adding the script to your Player, did you set the Jump Force in the Inspector?

1 Like