I am new to unity and I need your guidance.

I am learning how to make a 2D game through watching Youtube videos. Currently I am trying to learn to script 2D player movement.
In the given script I am supposed to have limited number of jumps (as much the amount I set) but still I am having infinite jumps.
Please Help !

Link to the video I used as a reference for learning :

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class CharacterController2D : MonoBehaviour
{
public float speed; //movement speed variable.
public float jumpForce; //at what force you will jump.
private float moveInput; //how to move?

private Rigidbody2D rb;

private bool facingRight = true; //for flipping character side by side when moving.

private bool isGrounded; //is character standing on ground? (ground check required!)
public Transform groundCheck;
public float checkRadius;
public LayerMask whatIsGround;

private int extraJump; //extra jumps!
public int extraJumpValue;

void Start()
{
extraJump = extraJumpValue;
rb = GetComponent();
}

void FixedUpdate() //to manage physics related aspects.
{
isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround); //is grounded or not?

moveInput = Input.GetAxis(“Horizontal”); //built-in unity input field equivalent to holding left or right keys.
rb.velocity = new Vector2(moveInput * speed, rb.velocity.y);

if (facingRight == false && moveInput > 0)
{
Flip();
}
else if(facingRight == true && moveInput < 0)
{
Flip();
}
}

void Update ()
{
if (isGrounded == true)
{
extraJump = extraJumpValue;
}

if (Input.GetKeyDown(KeyCode.UpArrow) && extraJump > 0)
{
rb.velocity = Vector2.up * jumpForce;
extraJump–;
}

else if (Input.GetKeyDown(KeyCode.UpArrow) && extraJump == 0 && isGrounded == true)
{
rb.velocity = Vector2.up * jumpForce;
}
}

void Flip() //flipping character side by side. one of the easy method!
{
facingRight = !facingRight;
Vector3 Scaler = transform.localScale;
Scaler.x *= -1;
transform.localScale = Scaler;
}
}

Forgive me if im wrong but i think this is more of a question for the Answers section Unity Discussions - A Space to Discuss All Things Unity
forums are more for discussions when really you just want to know an error in your code.

You also need to format your code, this is really hard to read and a lot of people wont bother to help at that point.

but at my guess, you need to set the extraJumpValue variable in the inspector. Its probably the default value 0.

1 Like

Not to contradict Brogan89, but from what I have read in the past the Answers page referenced isnt a great place as most people use these forums. So in my opinion this is in the right spot, but I could also be wrong. I just know you have a way higher chance of getting a response on these forums.

But, Brogan is probably right, you need to check the inspector. And, use code tags please:

2 Likes

Thats a good point. Fair enough. I used to go there a lot when I was learning, but now I never do lol

1 Like

Yeah, Answers is pretty much on life support.
Most people recommend asking for help here on the forums instead.

On topic:
Try reviewing the tutorial again. There’s a chance you may have accidentally missed something.

1 Like

My guess is that the LayerMask whatIsGround is not set.

1 Like

@kpman2300

Please format your code to use codetags… like already mentioned.

“I am supposed to have limited number of jumps (as much the amount I set) but still I am having infinite jumps.”

What is the radius of your “isGrounded” check OverlapCircle relative to your character size?

And have checked its placement so that it is in correct location?

1 Like

my actual mistake was that I didn’t make a layer dedicated to ground, now I fixed it. thanks for suggestions you provided.
However I wanna know something new right now (I won’t bother you with codes now, sorry about that, @Brogan89 was right.)

I want to “Fill” inside the square. but I am not able to get it.
Also, I was wondering, do I need to create a new thread everytime I have questions or doubts? I am afraid if this one becomes dead in the near future.

1 Like

“do I need to create a new thread everytime I have questions or doubts?”

Well if you have new question ask it in its own thread if it isn’t related to question you asked. Otherwise people who are looking for an answer to the same question will have hard time finding your thread…

Edit: Your original question isn’t clear either. If you can, edit it to something else? After all your question is about multi-jump not working - not about mistakes in typing or something.

1 Like

Yes I’ll do it now!

Whenever I turn left my character disappears, it is still there but you can’t see it. I tried flip option in sprite renderer but still nothing happened. what should I do?

Don’t set transform.eulerAngles directly (frankly, I don’t even know why the Unity team allowed it to be set in the first place).
All rotation operations in Unity are done with quaternions, and transform.eulerAngles is just meant to be a conversion from a quaternion rotation to a Euler rotation, which is easier to read, but setting the value will cause weird results, and is probably why your sprite is disappearing.

For setting a rotation, use Quaternion.Euler, which converts a Euler rotation value into a Quaternion:

if(moveInput > 0)
{
   transform.rotation = Quaternion.Euler(Vector3.zero);
}
else if (moveInput < 0)
{
   transform.rotation = Quaternion.Euler(0f, 180f, 0f);
}
1 Like

this didn’t work, it’s still happening!
I tried something new, I set Z axis scale to 0 and it worked!

Now, I am having a new problem, When I am turning my character, it does turn but it changes its position because it has a messed up pivot point and I am not able to bring it in center. My player is made by joining different parts.