• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by Kremnar · Mar 17, 2015 at 09:04 PM · 2d2d-platformer2d-physics2d-gameplay

How To Make My 2D Character Jump Gradually

Hey guys!

This is my first question on UnityAnswers. I'm new to game programming as well as the Unity engine and I'm currently working on my first game.

I've gotten my player to move around and jump, but the jumping is a bit jerky. The player rises a certain distance instantly, but then comes back to the ground normally, through gravity.

I would like the player to rise into the air a bit more gradually. As of now it seems to all be happening in one frame.

Here's the code I'm using.

 using UnityEngine;
 using System.Collections;
 
 public class PlayerControl : MonoBehaviour {
 
     // Running speed to be changed in inspector - this will be a part of the vector 'running'
     public float speed = 1.0f;
 
     // Jumping force to be changed in inspector - will be used to AddForce to the 2D RigidBody
     public float jump = 1.0f;
 
     // To detect if the player is on the ground or not
     bool onGround = true;
 
     // The vector to store out movement
     private Vector2 running;
 
 
     // Use for physics frame updates
 
     void FixedUpdate()  
     {   
         // Detects use of the right or left keys to apply a + or - 1 value to running.x
         running.x = Input.GetAxis ("Horizontal") * speed;
 
         // Executes running function through velocity
         GetComponent<Rigidbody2D>().velocity = running;
 
         // Listens for the player pressing the up button
         if(Input.GetKeyUp("up") && onGround)
         {
             // The character is now no longer on the ground
             onGround = false;
 
             // Makes the character jump
             GetComponent<Rigidbody2D> ().AddForce(new Vector2 (0, jump), ForceMode2D.Impulse);
 
         }
     }  
 
 
     // Checks if the player is touching the ground
     void OnCollisionEnter2D(Collision2D other) 
     {
         if (other.gameObject.tag == "Ground")
             onGround = true;
     }
 
 }

The player has a 2D RigidBody, no kinematics or triggers. As of now, my 'jump' variable is set at 50 and my gravity scale is 10.

Any help will be appreciated greatly and even if you want to comment on my code in general, I'm open to feedback. =)

Thanks in advance!

Comment

People who like this

0 Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image 82MiddleMan · Aug 28, 2015 at 01:26 AM 1
Share

I'm a beginner myself, but looking at your code, first I would set a rigidbody2d variable called rb2d. then in Start say rb2d = GetComponent<Rigidbody2d>(); this will improve performance as it only has to get component once, not twice every frame. For the jump, in update (not fixedUpdate or it might miss the input) I would then say

 if (onGround && Input.GetButtonDown ("Jump")){
                 onGround = false;
             rb2d.AddForce (new Vector2 (0, jump));
 }

I would also use an empty object at the feet of the character with a Physics2D.OverlapCircle to detect the ground. watch this tutorial its very useful link text.

Hope this helps.

2 Replies

· Add your reply
  • Sort: 
avatar image

Answer by gaaaalvez · Aug 28, 2015 at 07:04 AM

I made my character to jump with:

 this.GetComponent<Rigidbody2D>().velocity = new Vector2(0, some float here);


Before that I was using AddForce just as you, but I prefered the above method.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image

Answer by GiyomuGames · Aug 28, 2015 at 06:43 AM

When you add the force, use ForceMode2D.Force instead of ForceMode2D.Impulse (or nothing, it should work).

@82MiddleMan made good comments also on how to improve your code.

Comment

People who like this

0 Show 0 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Trying to implement a parry mechanic 0 Answers

How to store remaining durability of tiles? Tilemap 0 Answers

Bumper physics not working, 1 Answer

Death Counter dont work when changing scenes 2 Answers

Hello Im looking for help creating a script for spike or fire damage! 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges