Search Unity

Teleporting instead of Jumping

Discussion in 'Scripting' started by Bojacx, Feb 14, 2019.

  1. Bojacx

    Bojacx

    Joined:
    Jan 7, 2019
    Posts:
    4
    Hello, I'm currently trying to make a 2D platformer game, and I'm consistently having a problem with the jumping that does not seem to come up in tutorials in that my player object does not go up and then go back down. Instead, the object teleports a distance, and then falls back down. I also have a variable to track if the player is grounded or not, which is working fine.

    So, what I'm wondering is, why is my code teleporting the object instead of moving the player? Another thing I'm confused about as well is that when I try to translate the code for moving left and right, which can be seen right above Jump being called in FixedUpdate, to code to jump, it will still teleport me even though the same code that moves the object left and right is smooth.
     

    Attached Files:

  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Have you tried calling your jump function into an Update() rather than the FixedUpdate? Try that and lemme know what happens.
     
  3. Bojacx

    Bojacx

    Joined:
    Jan 7, 2019
    Posts:
    4
    When I call the Jump function in an Update, nothing happens when I press the space bar. I put in a Debug log to see if it was being read, and it is, so it is being read.
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Did you move all of your code into an Update() or just the Jump() function?
    I'm also curious about what happens when you press "W" or "S" using this code. I would imagine "Horizontal" would default those keys to work as well. Which may not be what you want. Worth a test, no?
     
  5. ManiaCCC

    ManiaCCC

    Joined:
    Aug 15, 2015
    Posts:
    41
    Just curious, what will happen, when you remove rigidBody velocity line.. will you still just teleport instead jumping?
     
  6. Bojacx

    Bojacx

    Joined:
    Jan 7, 2019
    Posts:
    4
    I moved all of my code from the Jump function to the Update function. Nothing is currently calling the Jump function.
    Also, the W and S keys do not do anything. "Horizontal" refers to left and right, and defaults to A and D. So with this code currently, they do not do anything.

    I do not 100% understand what you're saying. Do you mean remove the "velocity" part that is part of the line "rb2d.velocity = movement * speed"?
     
  7. Fatcat-Games

    Fatcat-Games

    Joined:
    Nov 9, 2016
    Posts:
    34
    You call rb2d.AddForce in Jump(), then in FixedUpdate() you set the rb2d.velocity = movement * speed; AddForce changes the velocity over time, but you are overwriting it in FixedUpdate().
    Try:
    Code (CSharp):
    1. rb2d.velocity += movement * speed;
     
    Bojacx likes this.
  8. Bojacx

    Bojacx

    Joined:
    Jan 7, 2019
    Posts:
    4
    This has worked perfectly now. Thank you so much. I had tried += with many different methods, however, it never occurred to me that they were all having to do with Vectors so it didn't work.