Force and Velocity problem
Hey,
Just started learning C# to port my 2D UE4 game to Unity, so far i'm learning quickly but while coding a jumping mechanic i'm encountering a weird issue that some of you might be able to explain.
Here's the code
//If on the ground and pressing jump, throw player in the air
if (grounded && Input.GetButtonDown ("Jump"))
{
isJumping = true;
body.AddForce (new Vector2 (0, jumpForce));
}
//If jumping, going up and releasing jump button, stop his upward velocity
if (Input.GetButtonUp ("Jump") && isJumping && body.velocity.y >= 0)
{
isJumping = false;
body.velocity = (new Vector2 (0, 0));
}
Everything works perfectly, however when i tap and release the jump button extremely quickly, the player jumps at full height ignoring the second if statement's velocity change.
I have used Debug.Log after every if statements and both get executed in the proper order.
So why isn't it setting the velocity to 0 when the button is being pressed and released very very quickly?
It's as if the AddForce takes more time to kick in, therefore when pressed extremely quickly the velocity gets changed before, it makes no sense to me.
Some help would be very appreciated!
Another example of what i don't understand
By adding these lines to the Start function
void Start ()
{
body.AddForce (new Vector2 (0, 500));
body.velocity = (new Vector2 (0,0));
}
I don't understand why the character is being thrown in the air, since i'm setting it's velocity to be zero right after i have added some force
Well, my post still hasn't been reviewed by a moderator.
Someone already helped me on the forums