I’m a High Schooler working through a class I bought off the internet. The level came with the assets. This specific part of the level won’t let me trigger a normal jump. It will trigger a double jump and then I’ll just be stuck there. The entire level uses the same collider but it lets me jump everywhere else. Any idea what’s wrong?
Nope, but I can give you some cool hints to help you figure it out.
Since you posted no code, I’m just going to reply very generally.
Very commonly the way a script determines “can I jump” is by checking for a collider under your feet.
This is done with raycast, spherecast, overlap sphere, some particular way out of a bunch of different ways.
Sometimes this is done by specifying a layer as well (google in Unity docs for layers and layer masks), so you could mark parts of the level as “jumpable” or “not jumpable” for instance.
If jumping works every except one place, one idea is to see how the script checks for jumping, and if it uses layers, then check the layers on the colliders located where you cannot jump, and see if they’re different.
In any case, you have some code, so here’s some more tricks and techniques below to figure out what is going on right while the game is running, which is one of the most amazing things about Unity: pause it and study what’s happening.
My standard debug logging blurb:
You must find a way to get the information you need in order to reason about what the problem is.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
you’re getting an error or warning and you haven’t noticed it in the console window
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 order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Another useful approach is to temporarily strip out everything besides what is necessary to prove your issue. This can simplify and isolate compounding effects of other items in your scene or prefab.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
Yeah, sorry I should’ve been more specific, I was in a bit of a rush when writing this question.
This is the code to control the player
public class PlayerController : MonoBehaviour
{
public float moveSpeed;
public Rigidbody2D RB;
public float jumpForce;
private bool isGround;
public Transform groundCheckPoint;
public LayerMask thisIsGround;
private bool doubleJump;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
RB.velocity = new Vector2(moveSpeed * Input.GetAxis("Horizontal"), RB.velocity.y);
isGround = Physics2D.OverlapCircle(groundCheckPoint.position, .2f, thisIsGround);
if (isGround)
{
doubleJump = true;
}
if(Input.GetButtonDown("Jump"))
{
if(isGround)
{
RB.velocity = new Vector2(RB.velocity.x, jumpForce);
}
else
{
if (doubleJump)
{
RB.velocity = new Vector2(RB.velocity.x, jumpForce);
doubleJump = false;
}
}
}
}
}
.
I set it so that it lets me jump when I’m on top of something on the ground layer. The entirety of the level is covered by a collider set to ground.
more components of the collider:
and yet this shelf on the level won’t let me jump on it.
If there’s anything else you’d need to see to help me figure this out I can show you it.
Don’t stop there! Hide that piece, then clone / copy a piece from somewhere else that you CAN jump on and move it where the hidden piece was. Can you jump now? If not, now we have a clue that it isn’t the ground. If you CAN, then you just had something wrong with the hidden piece. Investigate, understand, crosscheck, eliminate possibilities!