Search Unity

  1. Check out the Unite LA keynote for updates on the Visual Effect Editor, the FPS Sample, ECS, Unity for Film and more! Watch it now!
    Dismiss Notice
  2. The Unity Pro & Visual Studio Professional Bundle gives you the tools you need to develop faster & collaborate more efficiently. Learn more.
    Dismiss Notice
  3. Improved Prefab workflow (includes Nested Prefabs!), 2D isometric Tilemap and more! Get the 2018.3 Beta now.
    Dismiss Notice
  4. Improve your Unity skills with a certified instructor in a private, interactive classroom. Watch the overview now.
    Dismiss Notice
  5. Want to see the most recent patch releases? Take a peek at the patch release page.
    Dismiss Notice

Android mobile buttons jump character

Discussion in 'Scripting' started by xeonheart, Nov 11, 2018 at 12:36 AM.

  1. xeonheart

    xeonheart

    Joined:
    Jul 24, 2018
    Posts:
    36
    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 at 3:37 AM
  2. Reeii

    Reeii

    Joined:
    Feb 5, 2016
    Posts:
    67
    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:
    36
    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:
    67
    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.