Home > Blockchain >  Smooth Unity jump
Smooth Unity jump

Time:01-14

Could you please help me modifying this code, so that the jump become smooth. _rb.AddForce haven't worked for me like at all.

I know why is this happening, but dunno how to solve. Maybe some async tasks with a delay or deltaTime tricks?

Thanks.

private void Update()
{
    _moveInput = new Vector2(Input.GetAxisRaw("Horizontal"), 0);
    _moveVelocity = _moveInput * Speed;

    _jumpVelocity = new Vector2(0, 10) * 10f;


    if (_isGrounded == true)
    {
        if(Input.GetKeyDown(KeyCode.Space))
        {
            _isJump = true;
        }
    }

    if (_moveInput.x == 0)
    {
        anim.SetBool("isRunning", false);
    }
    else
    {
        anim.SetBool("isRunning", true);
    }
}

private void FixedUpdate()
{
    _rb.MovePosition(_rb.position   _moveVelocity);

    if (_isJump)
    { 
        _rb.MovePosition(_rb.position   _jumpVelocity * Time.fixedDeltaTime);
        Debug.Log("Jump");
        _isJump = false;
        _isGrounded = false;


    }
}

private void OnCollisionEnter2D(Collision2D collision)
{
    if (collision.collider.tag == "Ground")
    {
        _isGrounded = true;
    }
}

Async, AddForce, for with counters.

CodePudding user response:

Doesn't help.

void DoDelayMove(float delayTime)
    {
        _rb.MovePosition(_rb.position   _jumpVelocity);
        StartCoroutine(DelayAction(delayTime));
    }

    IEnumerator DelayAction(float delayTime)
    {
        //Wait for the specified delay time before continuing.
        yield return new WaitForSeconds(delayTime);

        //Do the action after the delay time has finished.
    }

CodePudding user response:

I had the same problem with the jump. One trick is to add your gravity. (Five times larger than the original ~ -50). I usually don't use the RigidBody. If you want to do so, then add this line

// removing gravity
rb.AddForce(0, -Physics.gravity.y, 0);
// applying gravity 5 times larger
rb.AddForce(0, Physics.gravity.y * 5, 0);

I suggest that you watch Brackeys video for more detailed information.

  • Related