Charactor Only jumps Once 2D

Hey Guys, first time poster and first time programmer of any kind so please forgive my slowness. I’ve searched a lot but maybe i’m not typing in the right words because i can’t seem to find a clear answer.

Been working on a simple Dino Run clone based on a tutorial, and most things seem to be working OK. but for whatever reason i can only get my character to jump once. The crouch function seems to be working fine.

here are my 3 scripts

Any help would be wildly appreciated been banging my head on this for about 7 hours and totally stuck.




Are you getting all of those debugs for jumping?

1 Like

8425137--1115127--upload_2022-9-8_6-26-43.png

Your game is over 1 second after the ground tag trigger, does that have something to do with it?

Use the inspector to see the value of isjumping, is it what you expect it to be? You’ll have to mark it is [SerializeField] to see it in the inspector, or make it public.

What’s the purpose of setting the jump gameobject to inactive? Is that intentional?

That’s simply because of the obstacles that are being generated every 1 second i can only jump once so the second obstacle will always end the game.

Forgive me how would i do that?

In the tutorial i followed that’s how it creates the ducking and jump functions and turns on and off each object/sprite. If that makes sense

thanks so much for your help i’m struggling man lol

Jump usually happens as part of a state change: eg, you jump and some piece of state is changed such that you can’t jump until you land again.

Start looking into how that state is supposed to be reset when you land.

If you have no idea how that works, then you have NOT actually done the tutorial correctly. You probably skipped Step #2 below.

Tutorials and example code are great, but keep this in mind to maximize your success and minimize your frustration:

How to do tutorials properly, two (2) simple steps to success:

Step 1. Follow the tutorial and do every single step of the tutorial 100% precisely the way it is shown. Even the slightest deviation (even a single character!) generally ends in disaster. That’s how software engineering works. Every step must be taken, every single letter must be spelled, capitalized, punctuated and spaced (or not spaced) properly, literally NOTHING can be omitted or skipped.

Fortunately this is the easiest part to get right: Be a robot. Don’t make any mistakes.
BE PERFECT IN EVERYTHING YOU DO HERE!!

If you get any errors, learn how to read the error code and fix your error. Google is your friend here. Do NOT continue until you fix your error. Your error will probably be somewhere near the parenthesis numbers (line and character position) in the file. It is almost CERTAINLY your typo causing the error, so look again and fix it.

Step 2. Go back and work through every part of the tutorial again, and this time explain it to your doggie. See how I am doing that in my avatar picture? If you have no dog, explain it to your house plant. If you are unable to explain any part of it, STOP. DO NOT PROCEED. Now go learn how that part works. Read the documentation on the functions involved. Go back to the tutorial and try to figure out WHY they did that. This is the part that takes a LOT of time when you are new. It might take days or weeks to work through a single 5-minute tutorial. Stick with it. You will learn.

Step 2 is the part everybody seems to miss. Without Step 2 you are simply a code-typing monkey and outside of the specific tutorial you did, you will be completely lost. If you want to learn, you MUST do Step 2.

Of course, all this presupposes no errors in the tutorial. For certain tutorial makers (like Unity, Brackeys, Imphenzia, Sebastian Lague) this is usually the case. For some other less-well-known content creators, this is less true. Read the comments on the video: did anyone have issues like you did? If there’s an error, you will NEVER be the first guy to find it.

Beyond that, Step 3, 4, 5 and 6 become easy because you already understand!

Also, photographs of code are not a thing. If you post a code snippet, ALWAYS USE CODE TAGS:

How to use code tags: https://discussions.unity.com/t/481379

I appreciate the reply, however my issue is not in following the tutorial. When i do it EXACTLY as described it works fine. but the tutorial does not use a different jump animation. I simply attempted to add my own jump animation using the same technique but it did not work. Is my issue that i split the objects into 3 different scripts?

Perhaps, but that’s just random speculation.

I stand by my first hypothesis above until you disprove it directly. See below.

Keep in mind that Animations support AnimationEvents, which are placed in the Animation itself to trigger scripts (eg, run code at specific points in the animation).

Perhaps the tutorial relies on this mechanism, and perhaps your cloned Animation neglected to call the same code?

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 supply a second argument to Debug.Log() and when you click the message, it will highlight the object in scene, such as Debug.Log("Problem!",this);

If your problem would benefit from in-scene or in-game visualization, Debug.DrawRay() or Debug.DrawLine() can help you visualize things like rays (used in raycasting) or distances.

You can also call Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene manually, looking for all the parts, where they are, what scripts are on them, etc.

You can also call GameObject.CreatePrimitive() to emplace debug-marker-ish objects in the scene at runtime.

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, such as this answer or iOS: https://discussions.unity.com/t/700551 or this answer for Android: https://discussions.unity.com/t/699654

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:

https://discussions.unity.com/t/839300/3

When in doubt, print it out!™

Running Script

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

public class chachirunningorig : MonoBehaviour
{
    Rigidbody2D rb;

    public GameObject crouch;
    public GameObject stand;
    public GameObject jump;

    //public GameObject jump;

    bool isjumping;
    bool isducking;
    bool isrunning;

    public GameManager gameManager;

    // Start is called before the first frame update
    void Start()
    {
        print("Chachi Running Script Start");
        rb = GetComponent<Rigidbody2D>();
        isjumping = false;
        isducking = false;
        isrunning = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("space") && isjumping == false)
        {
            print("is jumping");
            //rb.velocity = new Vector3(0, 25, 0);
            jump.SetActive(true);
            stand.SetActive(false);
        }

        if (Input.GetKey("down") && isjumping == false)
        {
            print("is ducking");
            isducking = true;
            stand.SetActive(false);
            crouch.SetActive(true);
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "ground")
        {
            print("running ground trigger");
            isjumping = false;
        }
        if (collision.tag == "obstacle")
        {
            print("game over");
            gameManager.GameOver();
        }
    }
}

Jumping Script

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

public class chachijumping : MonoBehaviour
{
    Rigidbody2D rb2;

    public GameObject stand;
    public GameObject jump;

    bool isjumping;

    public GameManager gameManager;

    // Start is called before the first frame update
    void Start()
    {
        print("jumping script go");
        rb2 = GetComponent<Rigidbody2D>();
        isjumping = true;
    }

    // Update is called once per frame
    void Update()
    { 
        //jumping here
        if (Input.GetKey("space") && isjumping == true)
        {
            print("is jumping 222");
            rb2.velocity = new Vector3(0, 28, 0);
            isjumping = false;
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.tag == "obstacle")
        {
            print("game over");
            gameManager.GameOver();
        }

        if (collision.tag == "ground")
        {
            print("ground tag triggered 2");
            isjumping = true;
            stand.SetActive(true);
            jump.SetActive(false);
        }
    }
}

Ducking Script

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

public class chachiducking : MonoBehaviour
{
    Rigidbody2D rb;

    public GameObject stand;
    public GameObject crouch;
    public GameObject jump;

    bool isducking;

    public GameManager gameManager;


    // Start is called before the first frame update
    void Start()
    {
        print("Chachi Ducking Script Start");
        rb = GetComponent<Rigidbody2D>();
        isducking = true;
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("space") && isducking == true)
        {
            print("is jumping2");
            isducking = false;
            jump.SetActive(true);
            crouch.SetActive(false);
        }

        if (Input.GetKeyUp("down"))
        {
            print("crouching complete");
            isducking = false;
            crouch.SetActive(false);
            stand.SetActive(true);
        }
    }

    void OnTriggerEnter2D(Collider2D collision)
    {
            if (collision.tag == "obstacle")
        {
            print("game over");
            gameManager.GameOver();
        }
    }
}

Excellent! Now break out your Debug.Log() and start figuring out why it is ignoring your jump intent.

We can’t do that for you, obviously.

When in doubt, print it out!™

1 Like

Thanks for the code block info also!

As for your advice I’m actually doing that, read your reply to another post before i posted and that’s why i’ve been adding in print statements as much as possible but i’m just not sure where to go from here.

So i’ve never done the debugging part before that is new to me can you reccomend any place to properly learn that?

How about right here right now!?

Steps:

  • identify where user intention is processed into a jump

  • put a log there to see if the code is even executing

  • if it is, find out why the player isn’t moving: log velocities etc.

1 Like

Sorry again but these terms are all completely brand new to me. Is using the print function the same as using the debug function? I’ve identified where i’m stuck (I think) but I can’t identify why. The ground Tag gets triggered which should activate the running script again and essentially restore the state of the character running before the jump but instead nothing happens.

But does it? Did you put a Debug.Log() inside that method?

ALSO: you can see the isJumping boolean. That’s the FIRST thing I would be printing every single frame.

Does it stay true even when you finish jumping? That’s it! FIND OUT WHY. Go to every line of code that changes it and put a Debug.Log() so you can understand which one is responsible for resetting it. Now make sure you don’t break that mechanism by adding a new animation.

I don’t know what that means. I don’t know how to write a debug.log() statement or where i put it in the code

So do i put

print(isjumping);

in the update phase to print that info every single frame?

Maybe i’m not understanding but it seems like either you didn’t see my print statements that i posted or i’m really stupid (probably the latter)

I added “Debug.Log();” to my code and it gave me this error

Your program works by:

  • accepting user intent to jump

  • making a decision if it can jump

  • jumping OR not jumping based on the above decision

I’m trying to draw your attention to investigate variables (such as what I suggested above, the isJumping variable) that may impeded the above process.

I’m not sure how much simpler I can make it. This is the PERFECT first debugging opportunity for you to get to the bottom of this issue.

If you are unable to grasp the parts of your program that do all of the above, then you haven’t actually “done” the tutorial. It is sounding more and more like you have not worked fully through Step #2 identified in my first response.

I get that. Truly i do.

I’m asking how do i use the debug function. I don’t know how to use it!

How on earth did you create Post #3 above!!! Try this in your Update() loop:

Debug.Log( "isJumping = " + isJumping);

and it will tell you what isJumping is every frame.