Hi guys… I need some help for scripting my 2D platformer
Im using rigidbody2D, and set gravity scale into 1
Im create walk script like this
if(Input.GetAxis("Horizontal") != 0)
{
velocityVector.x = Input.GetAxis("Horizontal") * moveSpeed;
velocityVector.y = transform.rigidbody2D.velocity.y;
transform.rigidbody2D.velocity = velocityVector;
}
and jump script like this
void jump()
{
if(!IsJumping)
{
_currentIncrement = 0;
rb.AddForce(Vector2.up * jumpPower);
IsJumping = true;
_isJumpCommandFinished = false;
StartCoroutine("jumping");
}
}
IEnumerator jumping()
{
while (!_isJumpCommandFinished)
{
_currentIncrement += jumpIncrement;
var jumpFactor = _currentIncrement / (maxJump - jumpPower);
if (jumpFactor < 1f)
rb.AddRelativeForce(Vector2.up * _currentIncrement, ForceMode2D.Impulse);
yield return null;
}
yield return null;
}
and heres the result
https://dl.dropboxusercontent.com/u/60462866/test/publish.html
but if you try jump and go to wall it will be stuck like this instead free fall with gravity

Im debug the rb.velocity (rb = this.rigidbody2D)
when jump the value is (0, 0…5) when jump, when fall (0, 0…-5)
when walk the value is (0…4, 0). when jump and push right (fly to wall) somehow the value is (4, 0) it means gravity no effect in here. but when right button is release the value of y decreasing like free fall
why the gravity has no effect when fly to wall and keep push right button ?? anyone have idea for this ??