简体   繁体   English

Unity 2D角色跳得太多

[英]Unity 2D Character Too Much Jumping

Firstly sorry for my bad english. 首先,抱歉我的英语不好。 My problem so clear; 我的问题很清楚;

My character sometimes jump to very high. 我的角色有时跳得很高。

Normally; 一般; gif; GIF; normal jump 正常跳跃

sometimes this happening if character jump to collider corner; 如果角色跳到对撞机角落,有时会发生这种情况; gif; GIF; anormal high jump 正常跳高

Why this happening? 为什么会这样呢? How I can fixed this problem? 我该如何解决这个问题?

that's my codes; 那是我的密码;

    private void FixedUpdate()
{
    jumpButton = GameObject.Find("Jump").GetComponent<Button>();
    jumpButton.onClick.AddListener(Jump);

    groundCheck = GameObject.Find("GroundCheck").GetComponent<Transform>();
    isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

    MoveInput = SimpleInput.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(MoveInput * speed, rb.velocity.y);

    if (isGrounded && jump)
    {
        rb.AddForce(Vector2.up * jumpForce, ForceMode2D.Impulse);
        jump = false;
    }
}

public void Jump()
{
    jump = true;
}

The way you have it, you are able to accelerate your upward movement everytime you jump. 通过这种方式,您每次跳跃时都可以加速向上运动。

In order to make the jump produce the same velocity each time, just set the y velocity to some value. 为了使每次跳跃产生相同的速度,只需将y速度设置为某个值即可。 We can use jumpForce/rb.mass to get the same value that using AddForce with ForceMode2D.Impulse produces. 我们可以使用jumpForce/rb.mass来获得与使用ForceMode2D.Impulse产生的AddForce相同的值。

private void FixedUpdate()
{
    jumpButton = GameObject.Find("Jump").GetComponent<Button>();
    jumpButton.onClick.AddListener(Jump);

    groundCheck = GameObject.Find("GroundCheck").GetComponent<Transform>();
    isGrounded = Physics2D.OverlapCircle(groundCheck.position, checkRadius, whatIsGround);

    MoveInput = SimpleInput.GetAxisRaw("Horizontal");
    rb.velocity = new Vector2(MoveInput * speed, rb.velocity.y);

    if (isGrounded && jump)
    {
        rb.velocity = new Vector2(rb.velocity.x, jumpForce/rb.mass);
        jump = false;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM