Search Unity

Android mobile buttons jump character

Discussion in 'Scripting' started by xeonheart, Nov 11, 2018.

  1. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    Hello,

    i am trying to find a good easy way to have my character which is a 2d jump when pressed, and wondering if you would rely on unity built in gravity or would you recommend any other script to handle the physics?
     
    Last edited: Nov 11, 2018
  2. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91
    If it has a rigidbody, you could easily add a force upwards using
    Code (CSharp):
    1. float jumpForce = 5;
    2.  
    3. public void Jump() {
    4.     rigidbody.AddForce(Vector3.up * jumpForce);
    5. }
     
  3. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    219
    good morning Reeii, big thank you, i do see my character going up slowly and the rigidbody 2d is not really doing its job of putting gravity on my character, do you recommend using rigid body2d? its weird, again the jump you have works, its moving my character up slowly, but never going down, it just stays there. should i find a gravity script something?
     
  4. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    91
    Hmm, do you have "is Kinematic" enabled on your rigidbody? If yes, disable it.
    Then change
    Code (CSharp):
    1. rigidbody.AddForce(Vector3.up * jumpForce);
    to
    Code (CSharp):
    1. rigidbody.AddForce(Vector3.up * jumpForce, ForceMode.Impulse);
    And you shouldn't need a gravity script since gravity is applied to every game object automatically if it has a rigidbody.