Search Unity

Unity Jump Script

Discussion in 'Scripting' started by neacsucristian98, Apr 20, 2019.

  1. neacsucristian98

    neacsucristian98

    Joined:
    Nov 30, 2018
    Posts:
    11
    Hello guys, I have a problem with my game, basically I can't make my player jump left or right just up. I would like it to jump up+left when pressing the left side of the screen and right+up when pressing the right one. Here is my script which doesn't work :
    rigidbody = GetComponent<Rigidbody2D>();
    rigidbody.velocity = new Vector3(100f,0f, 0f);
    rigidbody.AddForce(new Vector3(0, 10, 0) , ForceMode2D.Impulse);
    player.GetComponent<Animator>().enabled = false;
    player.GetComponent<Rigidbody2D>().gravityScale = 10f;

    THANK YOU GUYS!
     
  2. GroZZleR

    GroZZleR

    Joined:
    Feb 1, 2015
    Posts:
    3,201
    You're explicitly setting the velocity every frame, which overrides anything else you've set. Keep the current y velocity when you set the value and everything should work:
    Code (csharp):
    1.  
    2. rigidbody.velocity = new Vector3(100f, rigidbody.velocity.y, 0f);
    3.  
    Also, for future posts use code tags: https://forum.unity.com/threads/using-code-tags-properly.143875/
     
    neacsucristian98 likes this.