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 indie6 · Mar 19, 2014 at 04:23 AM · 2dplatformer

Fixed jumping in platformer jump

I am making a platformer game and implemented jumping, but I have an issue,when I multiply the JUMP VALUE with Time.deltaTime , the value is slightly different every time. Is it possible to make the player jump with same value every time the player jumps? Here is my code.

     void Update () {
     if(gameManager.paused) return;
     
     isPressed  =  false;
     
    if (Input.GetKey(KeyCode.LeftArrow) || isBtnLeft) 
     {
         isPressed = true;
         xSpeed -= (SPEED_STEP*Time.deltaTime);
         
         xSpeed = xSpeed<-MAX_X_SPEED ?-MAX_X_SPEED:xSpeed;
     }
     if (Input.GetKey(KeyCode.RightArrow) || isBtnRight)
     {
         isPressed = true;
         xSpeed += (SPEED_STEP*Time.deltaTime);
         
         xSpeed = xSpeed>MAX_X_SPEED ?MAX_X_SPEED:xSpeed;
     }
     

     if (Input.GetKeyDown(KeyCode.UpArrow) || isBtnJump){
             if(!isJumping){
                 isBtnJump = false;
                 isJumping = true;
                 squish(0.5f,1.4f);
                 float currentJumpSpeed = (jumpSpeed*Time.deltaTime);
                 currentJumpSpeed = currentJumpSpeed>MAX_JUMP_SPEED ? MAX_JUMP_SPEED:currentJumpSpeed;
                 ySpeed += jumpSpeed;                
             }
         }else
             ySpeed -=(gravity*Time.deltaTime);
     
     ySpeed = ySpeed<MAX_FALL_SPEED ? MAX_FALL_SPEED:ySpeed;
     
     
      if (Mathf.Abs(xSpeed)<1&&! isPressed) {
             xSpeed=0;
      }
     
     
     collisionY();
     collisionX();
     
     
     xSpeed *=hFriction;
     
     
 }
         }



What if i dont multiple the jumpSpeed with delta time?

Thanks

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
0

Answer by supernat · Mar 19, 2014 at 04:55 AM

I assume you have code that is decreasing the Y speed value every frame, accounting for gravity. If so, your jump speed should not be based on time. Let's call it an initial input velocity instead. If you jumped with an initial velocity of say 20 ft/s every time, then you would start in frame 0 of the jump with an upward velocity of 20, and each frame thereafter, the upward velocity would decrease by the acceleration over time. In other words, you would account for frame time when determining how far the player has fallen back to the ground between two frames, but the first frame would always be 20 ft up. So I think you just want to set an "initial velocity" up (no frame time concerns) and then subtract from that each frame based on the change in velocity since the last frame due to gravity, which is just the gravity (m/s)/s multiplied by deltaTime. Then move the player down a distance by the "current velocity" each frame, which again is just velocity (m/s) multiplied by deltaTime.

Comment
Add comment · Show 6 · 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 indie6 · Mar 19, 2014 at 11:39 AM 0
Share

Thanks a lot for the reply! in short, I just have to multiply the gravity with DeltaTime right?and then subtract it from the ySpeed?

Another question, is it possible to skip update for one frame? I have a bouncy platform, when the player touches it, the player bounces up. In this case, if I apply the bounce speed without multiplying with delta time, there are chances that gravity is also being applied in that frame. So how can I skip that?

avatar image supernat · Mar 19, 2014 at 06:12 PM 0
Share

It depends. Are you doing your own gravity? If so, then yes, multiply gravity time deltaTime, then add that value to your current velocity. Then multiply velocity by deltaTime, which gives you distance moved between frames, and add that distance to your position.

It sounds like you're relying on the physics engine though, in which case the only way to skip using gravity is to turn Is$$anonymous$$inematic on for a frame for the character and off the next. Actually you could probably also force the velocity of the character to a specific value (i.e. the 20 above) in the LateUpdate() method which is called after physics calculations have occurred.

If you are using the physics engine, you don't need to control your own velocity, gravity, etc. All you need to do is add an input force to the character and let the physics do the rest. I'm not sure I follow the question about bouncing. If you were to hit a bouncy platform, would the player really detect a single frame difference due to gravity? I mean your height is dependent on the position of the platform when the bounce starts to begin with. If you're using the physics engine bounce, I don't know, I don't really find it very consistent. I like to detect a collision with the platform and add my own upward force so that it consistently moves the same amount in a bounce.

avatar image indie6 · Mar 20, 2014 at 01:40 AM 0
Share

I am doing my own physics, I am adding my own upward force like you just said, but that upward force is added when the player collides with the bounce platform not the update function. Due to that I don't know in which line the Update function will be executing when the collision happens.

What if I move everything to fixedUpdate? Will that fix the issue? and any side effects of that?

avatar image supernat · Mar 20, 2014 at 03:35 AM 0
Share

FixedUpdate is a good place to handle physics generally speaking, so I would put everything related to forces in there. You still have to multiply your rates by the fixed time, but what this means is that they become constants. I still prefer to multiply my rates by the fixedDeltaTime value, because then you can adjust the physics time step size without changing your code. Some will suggest that you can just add velocities without concern for time (even Unity suggests this in the reference material), but to me that doesn't make sense because it breaks when you adjust the time steps, because then you have to adjust the constants you add in FixedUpdate too. I think it's always best to go with a fixed frame time though when you can, so yes.

I may be missing something. You said the upward force is added when you collide with the platform but not the update function. I don't think you would ever add forces from update, unless it was based on a flag or a constant force. But you mean in your OnTrigger or OnCollision method, you are adding the force due to the bound (please explain exactly how that related to the posted code, or update your post with that code), and in your update method (I assume above) you are adding force based on pressing of a key. Are both places updating the yspeed variable? Are you setting isJumping to true when you hit the bouncy platform?

avatar image indie6 · Mar 20, 2014 at 04:10 AM 0
Share

I have updated the code in first post, and yes i am updating ySpeed based on keypress. Here is Trigger Function of the bouncy platform.

 public void OnTriggerEnter(Collider collider){

     if(collider.gameObject.name.Equals("playerCollider")){

         player.onJumpPlatform(JU$$anonymous$$P*Time.deltaTime);

     }
 }

// in player class public void onJumpPlatform(float jumpValue){ isJumping = true; ySpeed = jumpValue; }

So multiplying the gravity and jump speed with fixedDeltaTime wont really have an effect on the game? In platformer games, why do some people multiply values with deltaTime? Shouldn't it always be fixed?Because I always want the jump to be of the same height.

Show more comments

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

22 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

Related Questions

Player color change on collision -1 Answers

Staring a 2D game 2 Answers

Change gravity on button 1 Answer

move, destroy self, instantiate again, repeat. 2 Answers

2D - Detect Slopes x Walls 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