Search Unity

Need help with jump...

Discussion in '2D' started by KillerDeathGiver, Sep 22, 2018.

  1. KillerDeathGiver

    KillerDeathGiver

    Joined:
    Sep 22, 2018
    Posts:
    3
    m having trouble currently with the jump, I dont get any errors but my character doesnt jump, further inspections is that all the lines with "Vector2" is labeled green in visual studio as well as the first if statement - please help :(

    public class SpritePlayerBehaviour : MonoBehaviour {

    public int playerSpeed = 10;
    private bool facingRight = true;
    public int playerJumpPower = 1250;
    private float moveX;

    // Update is called once per frame
    void Update() {
    PlayerBehaviour();
    }

    void PlayerBehaviour() {
    //Controls
    moveX = Input.GetAxis("Horizontal");
    if (Input.GetButtonDown("Jump"))
    {
    Jump();
    }
    //Animations
    //Character Direction
    if (moveX > 0.0f && facingRight == false)
    {
    FlipPlayer();
    }
    else if (moveX < 0.0f && facingRight == true)
    {
    FlipPlayer();
    }
    //Physics
    gameObject.GetComponent<Rigidbody2D>().velocity = new Vector2(moveX * playerSpeed, gameObject.GetComponent<Rigidbody2D>().velocity.y);
    }

    void Jump()
    {
    //Jump
    GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);
    }

    void FlipPlayer()
    {
    facingRight = !facingRight;
    Vector2 localScale = gameObject.transform.localScale;
    localScale.x *= -1;
    transform.localScale = localScale;
    }
    }
     
  2. DeanTheodorakis

    DeanTheodorakis

    Joined:
    Apr 29, 2018
    Posts:
    53
    Okay, so this part looked a little wrong to me, though disclaimer that I'm not very experienced myself:

    GetComponent<Rigidbody2D>().AddForce(Vector2.up * playerJumpPower);

    I looked up the example code in the scripting API here (link)

    there they make a private rigidbody2D with a name:

    private Rigidbody2D rb2D;

    and then inside the awake they add the component:

    rb2D = gameObject.AddComponent<Rigidbody2D>();

    and in update they use the name of their rigid body and add force to *that*:\

    rb2D.AddForce(transform.up * thrust);

    but you would change it to

    rb2D.AddForce(Vector2.up * playerJumpPower);

    Or if you wanted to stick closer to the example code you could try

    rb2D.AddForce(transform.up * playerJumpPower);

    I'll have to look more into Vector2.up to see how it works to know which is better, (which I don't know if I can do tonight) but hopefully one will work? you can check out the documentation or scripting API for more information, its a great source of info.

    It's just something to try.
     
    vakabaka likes this.
  3. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    I think, the script should be ok. Take a screenshot from the inspector of the rigidbody. Maybe something wrong with its settings.

    and yes, I whould make reference as DeanTheodorakis said.

    the difference between vector2.up and transform.up is that the transform.up is local vector and shows the "up" from the object and vector2.up is the "up" from the world.
     
    DeanTheodorakis likes this.