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 Loucims · Sep 05, 2020 at 11:42 AM · beginnerphysics2djumpingcharacter movement

When my character jumps, he does a little bounce before doing the jump Unity 2D,

I am still very new to Unity, so this might be a dumb question, but I have spent some time trying to create a 2D Character controller from the ground up for a school project I'm working on, and the movement works just fine, but whenever it comes to jumping, my character does a small bounce before jumping,

This is a gif of what happens whenever my character jumps: (https://i.gyazo.com/735264eaeb46b62ae44937cf5bc07a9a.mp4)

My character movement script is the following:

 public class PlayerMovement : MonoBehaviour
 {
     public Rigidbody2D rigidbody;
     public BoxCollider2D collider2D;
     [SerializeField] LayerMask groundLayer;
 
     public float JumpStrenght;
     public float SideStrenght;
 
     float SideMovement;
     float UpwardsMovement;
 
 
     void Update() {
         SideMovement = Input.GetAxis("Horizontal") * SideStrenght;
         UpwardsMovement = Input.GetAxis("Vertical") * JumpStrenght;
     }
     private void FixedUpdate() {
         MoveAround(SideMovement);
         JumpAround(UpwardsMovement);
     }
 
 
     void MoveAround(float xAxis) {
         rigidbody.velocity = new Vector2(xAxis, rigidbody.velocity.y);
     }
 
     void JumpAround(float yAxis) {
         if (isGrounded() && yAxis != 0) {
             Debug.Log("got to jump");
             rigidbody.velocity = new Vector2(rigidbody.velocity.x, yAxis + 1); 
         }
     }
 
     bool isGrounded() {
 
         RaycastHit2D hit2D = Physics2D.BoxCast(collider2D.bounds.center, collider2D.bounds.size, 0f, new Vector2(0, -1), 0.10f, groundLayer);
 
         if (hit2D.collider != null) {
             return true;
         }
         else {
             return false;
         }
     }
 }


I use a BoxCast to know wheter or not my character is grounded, and that works out alright, but the jump is the only thing that has gotten me scrambling my brains over. I'd really appreciate some help

Comment
Add comment · 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 ShadyProductions · Sep 05, 2020 at 11:42 AM 0
Share

Could be because you're setting the velocity directly, instead of using AddForce

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by mbro514 · Sep 05, 2020 at 03:53 PM

I think that I see what the problem is. With Input.GetAxis(), the value goes from 0 to 1 or -1 over a set period of time, and not just one frame. However, your character jumps during the first frame when the jump key is pressed, when the Vertical axis' value is only something like 0.1785, for example. Thus, at first, your character is only jumping 0.1785x the regular jumpheight. But by the time your character reaches the ground, the Vertical axis' value up to 1, and he does a regular jump. (I hope that I explained this well enough.)

To fix this, I would jump using a button rather than an axis (Unity automatically has a "Jump" button set up in the Input Settings.) In your script, I would write this:

 bool isTryingToJump;
 
 void Update()
 {
 SideMovement = Input.GetAxis("Horizontal") * SideStrenght;
 isTryingToJump = Input.GetButtonDown("Jump");
 }

And:

 // The yAxis parameter for this function isn't needed anymore
 void JumpAround() {
          if (isGrounded() &&  isTryingToJump) {
              Debug.Log("got to jump");
              rigidbody.velocity = new Vector2(rigidbody.velocity.x, jumpStrenght); 
          }
      }


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

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

My jumping script isn't working, And it isn't giving me any errors,My character isn't jumping, But I don't get any errors 1 Answer

Why is enemy jumping at different speed even though the velocity is the same? 0 Answers

Problems with Jumping 0 Answers

Movement of character by holding the up arrow instead of repeatedly pressing it. 1 Answer

Character jumps unusually high near platform edges and unusually far when repeatedly tapping movement keys 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