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
1
Question by wfin · Jan 08, 2021 at 03:57 PM · bugjumpingplatformerissue

Player's jump is random and inconsistent...

The title is very self-explanatory. The issue is that, whenever I jump, the player's jump height is very strange and random. I am basically making a platformer for a game jam and this issue is not very nice for platforming lol.

Here is the script:

using System.Collections; using System.Collections.Generic; using UnityEngine;

public class PlayerMovement : MonoBehaviour { public float speed = 10f; private Rigidbody2D rb; public float jumpForce = 800f;

 private bool allowJump;

 public Transform point;
 public LayerMask whatIsGround;
 public float radius = 0.5f;

 private void Start()
 {
     rb = GetComponent<Rigidbody2D>();
 }

 private void Update()
 {
     float x = Input.GetAxisRaw("Horizontal");
     rb.velocity = new Vector2(x * speed * Time.deltaTime, rb.velocity.y);

     allowJump = Physics2D.OverlapCircle(point.position, radius, whatIsGround);
     if (Input.GetButtonDown("Jump") && allowJump == true)
     {
         rb.AddForce(Vector2.up * jumpForce * Time.deltaTime, ForceMode2D.Impulse);
     }

     else 
     {
         allowJump = false;
     }
 }

}

so basically, I have some simple horizontal movement and some basic jumping. I have a bool that detects the ground. If it is overlapping, it will allow the player to jump, if there is nothing overlapping, player cannot jump so as to stop the player from jumping in mid-air.

Pls help :)

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
1

Answer by FeedMyKids1 · Jan 08, 2021 at 04:24 PM

Hey, I think the issue is that your setting values to the Player's rigidbody directly with your side to side movement.

Try to do the side to side movement in a different way, like by using Rigidbody.MovePosition (x speed Time.deltaTime).

I think that alone might make it more consistent.

What might also be an issue is that .AddForce is compounding the force that is already on the rigidbody, so you could (in your jump portion) set the rigidbody.velocity to Vector3.zero.

https://docs.unity3d.com/ScriptReference/Rigidbody.MovePosition.html

  private void Update()
  {
      float x = Input.GetAxisRaw("Horizontal");
      rb.MovePosition (transform.position + (x * speed * Time.deltaTime);
 
      allowJump = Physics2D.OverlapCircle(point.position, radius, whatIsGround);
 
      if (Input.GetButtonDown("Jump") && allowJump == true)
      {
         rb.velocity = Vector3.zero; 
 
          rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
      }
      else 
      {
          allowJump = false;
      }
  }

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 Llama_w_2Ls · Jan 08, 2021 at 04:21 PM

I had the same issue. This question has been posted many times, but to save you some hassle, don't addForce. Instead, modify the velocity.y of the rigidbody. This will create a consistent and predictable jump height. @Pixelated_Studioz

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 FeedMyKids1 · Jan 09, 2021 at 01:31 PM 1
Share

Hi,

The problem that immediately comes to $$anonymous$$d when directly modifying the velocity of the rigidbody is that you take away all acceleration and smoothing.

The answer I posted also modifies velocity and is kind of a hack but would work like what you're talking about.

Another approach would be to use NEGATIVE force equal to the rigidbody's up velocity:

 Vector3 negVelocity = rb.velocity * -1f;
 rb.AddForce (negvelocity * Time.deltaTime, Force$$anonymous$$ode.Impulse);
avatar image
0

Answer by wfin · Jan 09, 2021 at 04:19 PM

It didn't fix the bug still :( but thanks anyways. I have noticed something, when I don't multiply my jump power with Time.deltaTime, my player seems to jump fine (It doesn't sky rocket or jump inconsistently)... Why is that so? I have looked around and found nothing related to this..

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 Llama_w_2Ls · Jan 09, 2021 at 05:07 PM 1
Share

Time.deltaTime varies depending on your frames per second. It fluctuates, which would cause inconsistent jumping motions, i'm guessing.

avatar image FeedMyKids1 · Jan 09, 2021 at 09:33 PM 1
Share

Oh man, that's a huge overlapse on my part:

For the impulse type Force$$anonymous$$ode on AddForce, you don't want to use Time.deltaTime because like Llama2 says, it fluctuates based on the difference in the amount of time it took to render the last two frames.

AddForce (Force$$anonymous$$ode.Impulse) will add a one time force to the rigidbody which, with gravity, will dissipate over time and that's all controlled from within the Rigidbody class itself.

https://docs.unity3d.com/ScriptReference/Rigidbody.AddForce.html

avatar image
0

Answer by VeryAnnoyingCat · Jan 29, 2021 at 04:47 PM

@Pixelated_Studioz

SO,

  1. Time.deltaTime is the time elapsed in seconds since the last frame, which depends on framerate, and is therefore, inconsistent as pointed out previously. ForceMode.Impulse adds the entire force in a single frame. So deltaTime causes spikes in the jump force.

  2. Why da frig is there an else statement setting allowJump to false? In my sense the code would not mess up, but my sense is flawed so it is better to trust Debug.Log and remove the else statement. It is not needed for the code to function. So, the final code is:-

      private Rigidbody2D rb;
         public float jumpForce;
         public float speed;
         public bool allowJump;
         public Transform point;
         public LayerMask whatIsGround;
         public float radius = 0.5f;
         private void Start()
         {
             rb = GetComponent<Rigidbody2D>();
         }
         private void Update()
         {
             float x = Input.GetAxisRaw("Horizontal");
             rb.velocity = new Vector2(x * speed, rb.velocity.y);
     
             allowJump = Physics2D.OverlapCircle(transform.position, radius, whatIsGround);
     
             if (Input.GetKeyDown(KeyCode.UpArrow) && allowJump == true) // set this to your desired input method
             {
                 Debug.Log("bum");
                 rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
                 allowJump = false;
             }
         }
    
    

Also, consider using Physics2d.OverlapArea instead of OverlapCircle.

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

136 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 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

Input Axis Vertical is not set up? 1 Answer

Sprites are being cut in half. HELP! 4 Answers

Character Instantly hits Ground after Falling off Object 1 Answer

Smooth Crouch Movement Issue 0 Answers

2D jumping raycast question 0 Answers


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