Jumping in 2D doesnt work

Hi, i am very new to unity. I watched some tutorials and managed to get my character move left in right, but jumping doesn’t work though. I did everything right with the tutorials but it doesn’t do anything when i press space. Please help :frowning:

private float horizontal;
private float speed = 8f;
private float jumpingPower = 16f;
private bool isFacingright = true;

[SerializeField] private Rigidbody2D rb;
[SerializeField] private Transform groundCheck;
[SerializeField] private LayerMask groundlayer;

public bool Objectgrounded { get; private set; }


// Update is called once per frame
void Update()
{

    

    horizontal = Input.GetAxisRaw("Horizontal");

    if (Input.GetButton("jump") && isGrounded())

    {
        rb.linearVelocity = new Vector2(rb.linearVelocityX,jumpingPower);
    }

    if (Input.GetButtonUp("Jump") && rb.linearVelocityY < 0f)
    {
        rb.linearVelocity = new Vector2(rb.linearVelocityX, rb.linearVelocityY * 0.5f);
    }


    Flip();
}


private void FixedUpdate()
{
    rb.linearVelocity = new Vector2(horizontal * speed, rb.linearVelocity.y);
}

private bool isGrounded()
{
    return Physics2D.OverlapCircle(groundCheck.position, 0.2f, groundlayer);

}

private void Flip()
{
    if (isFacingright && horizontal < 0f || !isFacingright && horizontal > 0f)
    {
        isFacingright = !isFacingright;
        Vector3 localScale = transform.localScale;
        localScale.x *= 1f;
        transform.localScale = localScale;


       
    }

}

}

Sounds like you wrote a bug… and that means… time to start debugging!

By debugging you can find out exactly what your program is doing so you can fix it.

Use the above techniques to get the information you need in order to reason about what the problem is.

You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.

Once you understand what the problem is, you may begin to reason about a solution to the problem.

Remember with Unity the code is only a tiny fraction of the problem space. Everything asset- and scene- wise must also be set up correctly to match the associated code and its assumptions.

How to report your problem productively in the Unity3D forums:

http://plbm.com/?p=220

This is the bare minimum of information to report:

  • what you want
  • what you tried
  • what you expected to happen
  • what actually happened, log output, variable values, and especially any errors you see
  • links to actual Unity3D documentation you used to cross-check your work (CRITICAL!!!)

The purpose of YOU providing links is to make our job easier, while simultaneously showing us that you actually put effort into the process. If you haven’t put effort into finding the documentation, why should we bother putting effort into replying?

If you post code, only post the relevant code and always use the format button above. Do not post photographs of code.

1 Like

Okay thanks! These things are very new to me, so i didn’t really know how to ask help. I will try to do better :smiley:

Excellent… and WELCOME! We were all once just like you.

Here’s the strategy to keep sane and make steady progress: work one step at a time.

Just like this guy:

Imphenzia: How Did I Learn To Make Games:

Two steps to tutorials and / or example code:

  1. do them perfectly, to the letter (zero typos, including punctuation and capitalization)
  2. stop and understand each step to understand what is going on.

If you go past anything that you don’t understand, then you’re just mimicking what you saw without actually learning, essentially wasting your own time. It’s only two steps. Don’t skip either step.

2 Likes

Thank you very much!

1 Like