My character flies upwards instead of jumping (2D)
Greetings, I'm trying to make my 2D character jump by using AddForce, but i'm having an issue where even if i press the jump key a single time the character will keep flying upwards without going down despite not being in the jumping state anymore (almost as if it's in a zero-gravity space), i don't know what the problem exactly is and i would appreciate if anyone would provide some help. (also just in case you were wondering, i am using a dynamic rigidbody)
public class Movement2D : MonoBehaviour
{
public Rigidbody2D rb;
public float moveSpeed;
[SerializeField] public Vector2 HMove;
public bool isJumping;
public bool jumpSignal;
public float jumpDuration;
public float jumpExtra = 0.5f;
public float jumpForce = 0.4f;
void Awake()
{
rb = gameObject.GetComponent<Rigidbody2D>();
}
void Update()
{
HMove = new Vector2(Input.GetAxisRaw("Horizontal"), rb.velocity.y);
if (Input.GetButtonDown("Jump"))
{
jumpSignal = true;
}
else if (Input.GetButtonDown("Jump") == false) jumpSignal = false;
}
void FixedUpdate()
{
Move2D(HMove);
if (jumpSignal == true)
{
Jump();
}
}
void Move2D(Vector2 NavDir)
{
rb.MovePosition((Vector2) transform.position + (NavDir * moveSpeed * (Time.deltaTime)));
}
void Jump()
{
if (!isJumping)
{
isJumping = true;
jumpDuration = Time.time + jumpExtra;
rb.AddForce(new Vector2(0f, jumpForce) * rb.mass, ForceMode2D.Impulse);
print("jumped.");
} else if (isJumping)
{
if (jumpDuration <= Time.time)
isJumping = false;
}
}
}
Answer by PupperFish · Mar 14, 2021 at 09:05 PM
The reason it's making your character fly is because GetButtonDown only fires for one frame. The way it expects you to press a key is pressing it and then immediately letting go. The reason to your problem is also that you set a jump signal when you press the key and it will constantly make the the character go upwards until you press the key again.
Thanks for the info, I tried changing the input command into GetKey and it seems that the input registration works better now, and also fixed the jump signal so now that part looks like this
if (Input.GetKey(KeyCode.X))
{
jumpSignal = true;
}
else if (Input.GetKey(KeyCode.X) == false) jumpSignal = false;
However, the problem still persists, one thing worth mentioning is that my character also glides downwards when falling instead of just falling normally and i don't know what's the cause of this, i did try resetting the rigibody2d component in case it would help but it didn't solve anything @_@ .
I think the reason to why your character glides downwards is because in the Move2D function, you're constantly resetting the y value which causes the gravity to be slow.
Your answer
Follow this Question
Related Questions
Uneven Jump Heights 1 Answer
How do you make your player jump in the Roll-a-Ball tutorial series? 0 Answers
Problems with jumping in Doodle Jump like game 2 Answers
How do i move a cube like an actual cube?,How Can i Move a Cube? 0 Answers
How to double jump? i just can jump once when it touch the ground.. help me guys =D 1 Answer