Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by BluemanGreenworld · Jan 31, 2017 at 04:35 AM · 2dprogramming2d-physics

AddForce making jumping way too quick (2D)

I am trying to make my 2d character jump, and am using AddForce to do so. Whenever I jump however, the character accelerates way too fast, and then falls to the ground at normal speed, even though gravity is handled by a nearly exactly similar add force (Yes, i know that rigid bodies have gravity built in, but this creates the same exact problem). I have tried multiple different things, such as changing the velocity, instead of adding force, decreasing and raising the force, and every thing I've tried I still have the same result. Here is my code:

 using UnityEngine;
 using System.Collections;
 
 public class playerController : MonoBehaviour {
 
     [SerializeField]
     private float speed;
 
     [SerializeField]
     private float jumpHeight;
 
     [SerializeField]
     Transform[] groundPoints;
 
     [SerializeField]
     private LayerMask groundLayer;
 
     [SerializeField]
     private float overlapRadius;
 
     private bool grounded;
 
     [SerializeField]
     Rigidbody2D rb;
 
     void Start()
     {
 
     }
 
     void FixedUpdate()
     {
         movement();
         grounded = isGrounded();
     }
 
 
     void movement()
     {
         float yMove = jumpHeight;
         if (grounded & Input.GetButtonDown("Jump"))
         {
             rb.AddForce(new Vector2(0, yMove));
             Debug.Log(yMove);
         }
         rb.AddForce(Physics2D.gravity*10);
         float xMove = Input.GetAxis("Horizontal") * speed;
         rb.velocity = new Vector2(xMove, 0);
 
     }
     
 
     bool isGrounded()
     {
         foreach (Transform point in groundPoints)
         {
             Collider2D[] colliders = Physics2D.OverlapCircleAll(point.position, overlapRadius, groundLayer);
             for (int i = 0; i < colliders.Length; i++)
             {
                 if (colliders[i].gameObject != gameObject)
                 {
                     return true;
                 }
             }
         }
         return false;
     }
 
     }
Comment
Add comment · Show 3
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 hexagonius · Jan 31, 2017 at 04:58 PM 1
Share

first of all line 48 is wrong. You're adding force for jump and gravity and then you override y with 0. use the y velocity ins$$anonymous$$d. I'm not sure about 2 consecutive AddForce calls, I think I remember that not working very well but I could be mistaken, but you could apply jump $$anonymous$$us gravity force on jump and the other time only the gravity.

avatar image BluemanGreenworld hexagonius · Jan 31, 2017 at 06:35 PM 0
Share

Yes, I think that defaulting the y to zero could be the problem. I know that the 2 consecutive add force calls is fine, mainly because it is the same functionality of adding gravity via the rigidbody2D component, and also because it works the same way when doing so. Ill see if setting the rb.velocity = ( new Vector2( x$$anonymous$$ove, rb.velocity.y )) works.

avatar image BluemanGreenworld hexagonius · Jan 31, 2017 at 11:48 PM 0
Share

I made the changes you suggested and it fixed the problem right away, and i understand why as well. Thank you!

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Abdozai · Feb 01, 2017 at 11:18 AM

I can't help a lot but the way you made the jump is making it goes high wildly you can instead use add force to the rigid body with a new vector2 o 3 varribale

 //this function able my player to jump by adding force to his y axis
     void Jump(){
         rb2d.AddForce (new Vector2 (0,jumpForce));
     }

this a preview from my jump code the jump force has been set in the inspector to 950f but you can play with it till you get the effect you liked , it deppendes on your game Sorry for my english i can't write that good thank

this is my game movement script i re used all the time

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class PlayerCtrl : MonoBehaviour {
 
     public float maxSpeed;
     public float jumpForce;
     public bool isGrounded;
     public bool isFacingRight = true;
     public bool canMovWhileJumping;
     public Transform groundCheck;
     public LayerMask whatisGround;
     public float checkRadius = 0.2f;
     private Rigidbody2D rb2d;
     private Animator anim;
 
     void Awake () {
         rb2d = GetComponent<Rigidbody2D> ();
         anim = GetComponent<Animator> ();
     }
     //this function for physical stuff and calculation
     void FixedUpdate () {
         checkForGround ();
         Move ();
     }
 
     void Update(){
         anim.SetBool ("Grounded", isGrounded);
         if (Input.GetKeyDown (KeyCode.Space) && isGrounded) {
             Jump ();
         }
     }
     //this function moves the player
     void Move(){
         //this if stat stop the player from moving while he's jumping
         if (!isGrounded && !canMovWhileJumping)
             return;
         //here i used get axis to get a value between 0 and 1 when the player hit the right and left arrows
         float move = Input.GetAxis ("Horizontal");
         anim.SetFloat ("Speed", Mathf.Abs (move));
         //here my players move by effecting his rigidbody compement velocity
         rb2d.velocity = new Vector2 (move * maxSpeed, rb2d.velocity.y);
         if (move < 0 && isFacingRight)
             Flip ();
         if (move > 0 && !isFacingRight)
             Flip ();
     }
     //this function able my player to jump by adding force to his y axis
     void Jump(){
         rb2d.AddForce (new Vector2 (0,jumpForce));
     }
     //this function check if there is a ground under my player so ican make sure he is grounded
     void checkForGround(){
         isGrounded = Physics2D.OverlapCircle (groundCheck.position,checkRadius,whatisGround);
     }
     //this function flips the player to other direction right to left
     void Flip(){
         isFacingRight = !isFacingRight;
         Vector3 theScale = transform.localScale;
         theScale.x *= -1;
         transform.localScale = theScale;
     }
 }

hope this is helped you

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

131 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

Related Questions

What is the Easiest Way to Check if Two Time Periods are Overlapping 0 Answers

Object with rigidbody2D doesn't move when it's supposed to, 0 Answers

Constraning an object's forces to only two directions in 2D (or infinite friction?) 0 Answers

Using Weapons From Inventory In My 2D Game? 0 Answers

Collision between two 2D objects. 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