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 shyvert9 · Apr 07, 2021 at 06:36 PM · velocitygravityjump

Continuous Jumping using gravity

I can't understand why this code changes the player's speed when standing on a platform (so the speed should be 0). As a result, gravity also changes: alt text

This is velocity.y: alt text

The problematic function is this (if I remove it, velocity.y becomes 0 when the player is on a floor):

 private void setJumpGravity()
     {
         if(playerRb.velocity.y < 0)
         {
             playerRb.gravityScale = fallJumpGravity;
         }
         else if(playerRb.velocity.y > 0 && Input.GetKey(KeyCode.Space) == false)
         {
             playerRb.gravityScale = spaceJumpGravity;
         }
         else
         {
             playerRb.gravityScale = defaultGravity;
         }
     }

The whole class is:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerMovement : MonoBehaviour
 {
     private float horizontalInput;
     [SerializeField] private float horizontalSpeed;
     private Rigidbody2D playerRb;
 
     private bool isGetSpaceDown;
     [SerializeField] private float jumpForce;
     [SerializeField] private float fallJumpGravity;
     [SerializeField] private float spaceJumpGravity;
     private float defaultGravity;
 
     // Start is called before the first frame update
     void Awake()
     {
         playerRb = gameObject.GetComponent<Rigidbody2D>();
         defaultGravity = playerRb.gravityScale;
     }
 
     void Update()
     {
         horizontalInput = Input.GetAxisRaw("Horizontal");
 
         if(Input.GetKeyDown(KeyCode.Space))
         {
             isGetSpaceDown = true;
         }
     }
 
     // Update is called once per frame
     void FixedUpdate()
     {
         playerRb.velocity = new Vector2(horizontalSpeed * horizontalInput, playerRb.velocity.y);
 
         if(isGetSpaceDown == true)
         {
             playerRb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
             isGetSpaceDown = false;
         }
 
         setJumpGravity();
 
         //Debug.Log(playerRb.velocity.y);
         Debug.Log(playerRb.gravityScale);
     }
 
     private void setJumpGravity()
     {
         if(playerRb.velocity.y < 0)
         {
             playerRb.gravityScale = fallJumpGravity;
         }
         else if(playerRb.velocity.y > 0 && Input.GetKey(KeyCode.Space) == false)
         {
             playerRb.gravityScale = spaceJumpGravity;
         }
         else
         {
             playerRb.gravityScale = defaultGravity;
         }
     }    
 }
 

untitled.png (64.9 kB)
untitled-1.png (68.3 kB)
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

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by HellsHand · Apr 08, 2021 at 01:19 PM

Easy fix is to add a bool for whether or not the player is jumping. Add a private bool isJumping = false to your initializers.

     if (isGetSpaceDown == true)
     {
         playerRb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
         isJumping = true; //after adding upward force set isJumping to true
         isGetSpaceDown = false;
     }

Then change the section of code where you find the issue to:

 private void setJumpGravity()
 {
     if (isJumping)  //this will ensure gravity is only changed when in the air
     {
         if (playerRb.velocity.y < 0)
         {
             playerRb.gravityScale = fallJumpGravity;
         }
         else if (playerRb.velocity.y > 0 && Input.GetKey(KeyCode.Space) == false)
         {
             playerRb.gravityScale = spaceJumpGravity;
         }
     }
     else
     {
         playerRb.gravityScale = defaultGravity;  //now gravity will remain at it's default when not in the air
     }
     if(playerRb.velocity.y == 0)  //set isJumping back to false when the player hits the ground
     {
         isJumping = false;
     }
 }

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 shyvert9 · Apr 08, 2021 at 05:17 PM 0
Share

Thanks for the solution. I publish below another solution that I have found, maybe it can be useful to someone.

 private void setJumpGravity()
         {
             if(playerRb.velocity.y < 0)
             {
                 playerRb.gravityScale = fallJumpGravity;
             }
             else if(playerRb.velocity.y > 0 && Input.GetKey(KeyCode.Space) == false)
             {
                 playerRb.gravityScale = spaceJumpGravity;
             }
             if(isCollidingGround() == true)
             {
                 playerRb.gravityScale = defaultGravity;
             }
         }


where:

 private bool isCollidingGround()
     {
         BoxCollider2D playerCollider = gameObject.GetComponent<BoxCollider2D>();
         RaycastHit2D groundCastHit = Physics2D.BoxCast(playerCollider.bounds.center, playerCollider.bounds.size,
                                     0, Vector2.down, 0.1f, JumpableLayer);
 
         if(groundCastHit.collider == null)
             return false;
         return true;
     }




avatar image
0

Answer by singhlaxman9761 · Apr 08, 2021 at 12:35 PM

Ignore everything and use Physics material for jumping like bouncing ball :)

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

124 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

Related Questions

Anti-Gravity isn't working 1 Answer

how to jump at fixed height but faster 0 Answers

Jumping a specific height using Velocity & Gravity 1 Answer

Get the character jumps in his local position 0 Answers

i couldnt make my object jump.,i couldn't make my object jump 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