Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 /
  • Help Room /
avatar image
0
Question by Bioshok7 · Aug 11, 2017 at 07:20 AM · rigidbody2dvelocitygravityplatformer

how to fix velocity for jump and movement or fall in 2d pltform game with gravity

Hello everybody ,

Please help me and thank you ,

I use fixed update a movement function with rigidBody2D.Velocity to move my character in 2d platform game (with rigidbody2D.velocity = new vector 2 (movement on x, rigidBody2D.velocity.y) and another jump function Except to jump in addition to this function,

The problem is that gravity is either very slow with a simple jump or very fast with a big fall with sometimes the range of the jump that varies,

Please what is the simplest and fastest and most realistic and adequate formula to determine the rigidBody.velocity in this case and what is the most suitable 2d mode force in this case,

thank you infinitely and good day.

Cordially.

Comment
Add comment
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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by salamander555 · Aug 03, 2017 at 04:50 PM

Is it possible to paste your exact code? becaue I think you are just making a small mistake.

Comment
Add comment · Show 2 · 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 Bioshok7 · Aug 03, 2017 at 05:56 PM 0
Share

link text

mycodesample.txt (2.4 kB)
avatar image salamander555 Bioshok7 · Aug 03, 2017 at 06:47 PM 0
Share

when you add a force to you velocity.y try just doing this

 rigidbody.velocity = new Vector2 (rigidbody.velocity.x, "Jump Power")

this is a much easier way and the gravity should work automaticly on the object

avatar image
0

Answer by Bioshok7 · Aug 11, 2017 at 06:52 AM

/**Ok,Here is the code below (not complete it also misses may be block of code)*/

" void FixedUpdate ()
{

if ((!Input.GetKey (KeyCode.RightArrow) && !Input.GetKey (KeyCode.LeftArrow)) && Input.GetAxis ("L_XAxis_1") == 0 && Input.GetAxis ("DPad_XAxis_1") == 0) { h = 0; rigidBody.velocity = (new Vector2 (h, rigidBody.velocity.y)); anim.SetFloat ("speed", 0f); sons.audioSource.Stop (); } if (!Input.GetKey (modes.boutonX) && !Input.GetKey (KeyCode.Joystick1Button1)) { sons.audioSource.clip = sons.walk; if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) { h = 1; sons.audioSource.Play (44100); anim.SetFloat ("speed", 1f); } if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0) { h = -1; sons.audioSource.Play (44100); anim.SetFloat ("speed", 1f); } rigidBody.velocity = (new Vector2 (h moveForce, rigidBody.velocity.y));
anim.SetBool ("X", false); } if (Input.GetKey (modes.boutonX) || Input.GetKey (KeyCode.Joystick1Button1)) { sons.audioSource.clip = sons.run; if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) { h = 1; sons.audioSource.Play (44100); anim.SetFloat ("speed", 1f); } if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0) { h = -1; sons.audioSource.Play (44100);
anim.SetFloat ("speed", 1f);
} rigidBody.velocity = (new Vector2 (h
maxSpeed, rigidBody.velocity.y));
anim.SetBool ("X", true); } } } Jump (); }

//function flip too in Update ()

void Jump () {
if ((Input.GetKeyDown (modes.buttonSpace) || (Input.GetAxisRaw ("Jump") > 0)) && jump) { if (nombresaut >= 0) {
jumpNumber--; jumpOk = true;

             if (up)
                 rigidBody.AddForce (new Vector3 (0, -h1 * jumpForce * Time.deltaTime, 0), ForceMode2D.Impulse);        
             jump = false;                   
         }
     }    

     if (Input.GetAxisRaw ("Jump") == 0 && !Input.GetKeyDown (modes.boutonEspace) && !jump) {        
         jump = true;     
         jumpOK = true;
     }
 }

"

Comment
Add comment · 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
0

Answer by Bioshok7 · Aug 10, 2017 at 04:58 AM

Here is the code below (not complete it also misses may be block of code)`

 void FixedUpdate ()   
     {           
     
 if ((!Input.GetKey (KeyCode.RightArrow) && !Input.GetKey (KeyCode.LeftArrow)) && Input.GetAxis ("L_XAxis_1") == 0 && Input.GetAxis ("DPad_XAxis_1") == 0) {
                             h = 0;
                             rigidBody.velocity = (new Vector2 (h, rigidBody.velocity.y));
                             anim.SetFloat ("speed", 0f);
                             sons.audioSource.Stop ();
                         }
                         if (!Input.GetKey (modes.boutonX) && !Input.GetKey (KeyCode.Joystick1Button1)) {
                             sons.audioSource.clip = sons.walk;
                             if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) {
                                 h = 1;
                                 sons.audioSource.Play (44100);
                                 anim.SetFloat ("speed", 1f);
                             }
                             if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0) {
                                 h = -1;
                                 sons.audioSource.Play (44100);
                                 anim.SetFloat ("speed", 1f);
                             }
                             rigidBody.velocity = (new Vector2 (h * moveForce, rigidBody.velocity.y));  
                             anim.SetBool ("X", false);
                         }
                         if (Input.GetKey (modes.boutonX) || Input.GetKey (KeyCode.Joystick1Button1)) {
                             sons.audioSource.clip = sons.run;
                             if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) {
                                 h = 1;
                                 sons.audioSource.Play (44100);
                                 anim.SetFloat ("speed", 1f);
                             }
                             if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0) {
                                 h = -1;
                                 sons.audioSource.Play (44100);      
                                 anim.SetFloat ("speed", 1f);     
                             }
                             rigidBody.velocity = (new Vector2 (h * maxSpeed, rigidBody.velocity.y));         
                             anim.SetBool ("X", true);
                         }
                     }
 }
                     Jump ();
 }
 
 //function flip too in Update ()
 
     void Jump ()
     {           
         if ((Input.GetKeyDown (modes.buttonSpace) || (Input.GetAxisRaw ("Jump") > 0)) && jump) {
             if (nombrejump >= 0) {      
                 jumpNumber--;
                 jumpOk = true;                 
 
                 rigidBody.AddForce (new Vector3 (0, -h1 * jumpForce * Time.deltaTime, 0), ForceMode2D.Impulse);        
                 jump = false;                   
             }
         if (Input.GetAxisRaw ("Jump") == 0 && !Input.GetKeyDown (modes.buttonSpace) && !jump) {        
             jump = true;     
             jumpOK = true;
         }
     }
Comment
Add comment · 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
0

Answer by Bioshok7 · Aug 11, 2017 at 06:52 AM

 void FixedUpdate ()   
 {               
 if ((!Input.GetKey (KeyCode.RightArrow) && !Input.GetKey (KeyCode.LeftArrow)) && Input.GetAxis ("L_XAxis_1") == 0 && Input.GetAxis ("DPad_XAxis_1") == 0) 
                {
         h = 0;
         rigidBody.velocity = (new Vector2 (h, rigidBody.velocity.y));
                             anim.SetFloat ("speed", 0f);
                             sons.audioSource.Stop ();
         }
 if (!Input.GetKey (modes.boutonX) && !Input.GetKey (KeyCode.Joystick1Button1))
                 {
         sons.audioSource.clip = sons.walk;
                 if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) 
                                {
                 h = 1;
                 sons.audioSource.Play (44100);
                 anim.SetFloat ("speed", 1f);
                 }
                 if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0)
                                {
                 h = -1;
                 sons.audioSource.Play (44100);
                 anim.SetFloat ("speed", 1f);
                 }
         rigidBody.velocity = (new Vector2 (h * moveForce, rigidBody.velocity.y));  
         anim.SetBool ("X", false);
         }
         if (Input.GetKey (modes.boutonX) || Input.GetKey (KeyCode.Joystick1Button1)) 
                 {
         sons.audioSource.clip = sons.run;
                           if (Input.GetKey (KeyCode.RightArrow) || Input.GetAxis ("L_XAxis_1") > 0 || Input.GetAxis ("DPad_XAxis_1") > 0) 
                                  {
                 h = 1;
                 sons.audioSource.Play (44100);
                 anim.SetFloat ("speed", 1f);
                 }
                 if (Input.GetKey (KeyCode.LeftArrow) || Input.GetAxis ("L_XAxis_1") < 0 || Input.GetAxis ("DPad_XAxis_1") < 0) 
                                 {
                 h = -1;
                 sons.audioSource.Play (44100);      
                 anim.SetFloat ("speed", 1f);     
                 }
         rigidBody.velocity = (new Vector2 (h * maxSpeed, rigidBody.velocity.y));         
         anim.SetBool ("X", true);
         }
     }
         Jump ();
 }
 
 //function flip and Jump too in Update ()
 
 void Jump ()
 {           
                    if ((Input.GetKeyDown (modes.buttonSpace) || 
                   (Input.GetAxisRaw ("Jump") > 0)) && jump) 
                     {
                     if (jumpNumber >= 0) 
                             {      
                 jumpNumber--;
                 jumpOk = true;
                           }                 
                          if(jumpOk)
                         {
                 rigidBody.AddForce (new Vector3 (0, -h1 * jumpForce * Time.deltaTime, 0), ForceMode2D.Impulse);        
                  jump = false;                   
             }
               if (Input.GetAxisRaw ("Jump") == 0 && !Input.GetKeyDown (modes.buttonSpace ) && !jump) 
                       {        
             jump = true;     
             jumpOK = true;
               }
 }
Comment
Add comment · 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.

Follow this Question

Answers Answers and Comments

71 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

velocity and collider blocking gravity 0 Answers

My Unity Rigidbody 2D character fall slowly after a jump,Unity RigidBody Character falls slowly 0 Answers

Rigidbody2D falls slowly with MovePosition? 1 Answer

Had to turn off gravity scale, but now player goes through walls 0 Answers

2D Endless game - Best method for moving objects? 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