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;
}
}
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.
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.
I made the changes you suggested and it fixed the problem right away, and i understand why as well. Thank you!
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
Your answer
![](https://koobas.hobune.stream/wayback/20220612110002im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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