Search Unity

  1. Good news ✨ We have more Unite Now videos available for you to watch on-demand! Come check them out and ask our experts any questions!
    Dismiss Notice

Feedback Why my Code isn't working?

Discussion in 'Getting Started' started by Artikel_5, Jul 15, 2020.

  1. Artikel_5


    Jul 14, 2020
    I want to creat a Game like Doodle-Jump, but i really need some help. I'm pretty new into Unity and Visual Studio and stucking at this problem for hours. I want my player to Bounce on an Plattform, but the Player always lands on the Plattform. I watched many tutorials but nothing could help me. I would be very grateful, if someone knows what the Problem is.

    1. Player
    2. Plattform that is not Bouncing

    Screenshot_1.png Screenshot_2.png
  2. Vryken


    Jan 23, 2018
    Place some Debug.Logs to find out if the conditions you're checking for are actually true.

    My guess is that the player's y-velocity is still greater than 0 when it collides with the platform:

    Code (CSharp):
    1. void OnCollisionEnter2D(Collision2D collision) {
    2.    Rigidbody2D rb = collision.gameObject.GetComponent<Rigidbody2D>();
    4.    //Does the collided GameObject have a Rigidbody2D?
    5.    Debug.Log(rb);
    7.    if(rb != null) {
    8.       //If a Rigidbody2D does exist, what is its y-velocity at the time of collision?
    9.       Debug.log(rb.velocity.y);
    11.       if(rb.velocity.y <= 0) {
    12.          //If the y-velocity is <= 0, add upward force to the Rigidbody2D.
    13.          Debug.Log("Adding force");
    14.          rb.AddForce(Vector3.up * 600);
    15.       }
    16.    }
    17. }
    Last edited: Jul 15, 2020
  3. Artikel_5


    Jul 14, 2020
    Thank you very much, but now the player is bouncing one time and at the second time he lands on it.
    Also i've add an Gameobject that should destroy the Plattforms that are out of Camera-Range but i cant drop my Player and my Plattforms intro the script. I went step by step with an tutorial for an Doodle-Jump-Game. For me it looks like Unity doesn't refresh my Script.
    1. How it is
    2. How it should be
    3. The Script that's used

    Sorry for asking something like that, but i'm pretty new to Unity. To be exact, i started two days ago.

    Screenshot_3.png Screenshot_4.png
  4. JoeStrout


    Jan 14, 2011
    You're wise to start with a simple game like Doodle Jump. Some general advice:

    • Don't paste code as screen shots. Paste it as code, like @Vryken did above (use the "Code:" button in the editor toolbar).
    • Don't expect anyone to lead you by the hand, step by step. Game development requires determination and drive.
    • Don't "watch" tutorials; that teaches you almost nothing. Do tutorials. It could be that you're not ready to create your own project from scratch because you haven't actually done any tutorials yet.
    • Ask one question at a time. In this thread you started off asking about why your character wasn't bouncing, but then seem to have switched to asking about destroying platforms as they fall out of camera range (and I can't make out what your problem is with that).
    Hang in there, do some tutorials, post clear and well-formed questions, and you'll get there soon enough!
  5. asherdavidson


    Aug 17, 2020
    Thanks for your advice!