Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by TehWut · Jan 03, 2013 at 11:48 PM · rigidbodyvelocityjump

rigidbody.AddForce jump, different heights each time

Hello, I would think that my code would work fine for jumps, but each time I jump the height is different. It changes with x velocity for some reason, is there some way to make it consistent? could it have something to do with the Delta time?

relevant code:

 function Update () {
 
 animTimer++;
 
 rigidbody.AddForce(Input.GetAxis("Horizontal") * horizontalSpeed * Time.deltaTime * 60,0,0);
 rigidbody.velocity.x = Mathf.Clamp(rigidbody.velocity.x,-6,6);
 
 if (Input.GetKeyDown(KeyCode.UpArrow) && isGrounded)
     {
         rigidbody.AddForce(0,jumpSpeed * Time.deltaTime * 60,0);
     }
 
             if (animTimer >= animSpeed)
                 {
                     frame++;
                     animTimer = 0;
                 }
                 
 if (Input.GetAxis("Horizontal") < 0) //if I'm pressing right
     {
     lastKeyPressed = "left";
         if (isGrounded)
                 {
         animLayer = 0;
                 }
         else
                 {
         animLayer = 4;
                 }
         
     }
 if (Input.GetAxis("Horizontal") > 0) //if I'm pressing right
     {
     lastKeyPressed = "right";
         if (isGrounded)
             {
     animLayer = 1;
             }
         else
             {
     animLayer = 5;
             }
         
     }
 
 if (!Input.GetAxis("Horizontal") && isGrounded) //if I'm pressing right
     {
         if (lastKeyPressed == "right")
             {
                 animLayer = 2;
             }
         if (lastKeyPressed == "left")
             {
                 animLayer = 3;
             }
     animTimer = 0;
     frame = 0;
     }
 
     rigidbody.velocity.x = rigidbody.velocity.x * friction;
         
         renderer.material.mainTextureOffset = Vector2(xOffset * frame, yOffset *animLayer);
 }
 
 function OnCollisionStay()
     {
         isGrounded = true;
     }
     
 function OnCollisionExit()
     {
         isGrounded = false;
     }
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

1 Reply

· Add your reply
  • Sort: 
avatar image
2

Answer by Lovrenc · Jan 03, 2013 at 11:55 PM

You dont need deltaTime in jump. It is not continuous operation, you just add jump force once. If for some reason deltaTime is huge (lets say huge explosion is happening and framerate dropped significally), you would jump much higher.

Just delete deltaTime in jump and set jump force to suit your needs.


EDIT: This is why you should do PHYSICS stuff in FixedUpdate rather than Update. Your code would work perfectly there (even though deltaTime and jump isnt good idea, unless you maybe had thrusters on your feet) as that is asynchronously called 50 times per second. FixedUpdate was made for this.

Comment
Add comment · Show 1 · 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 TehWut · Jan 04, 2013 at 12:02 AM 0
Share

I have no issues thus far, thanks for the tip

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

10 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

Related Questions

Getting rigidbody to jump by changing the velocity 2 Answers

Jumping from a conveyor belt with added velocity 0 Answers

Setting Rigidbody Velocity for Jump Unity 5 0 Answers

ForceMode.Impulse Doesnt always fire. 2 Answers

how to limit horizontal input when jumping 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