首页 > 解决方案 > 在统一 2d 中,我的角色有时会跳得太厉害

问题描述

我的角色跳跃在直线对撞机中运行良好,然后我注意到每次我的角色在曲线形对撞机中跳跃时,我的跳跃总是异常强烈。我怎样才能解决这个问题?我将在下面提供跳跃运动的初始代码。

这是img:

在此处输入图像描述

这是跳转代码:

float checkgroundradius = 0.50f;
public bool isgrounded2;
public Transform grouncheckslot;
public LayerMask LM;
public float JumpPower;

public float movespeed;
Rigidbody2d RB;

void Update () {

    if (isgrounded && Input.GetKeyDown(KeyCode.UpArrow))
    {
        isgrounded = false;
        jumping = true;
        myanim.SetBool("groundedanim", isgrounded);
        myrb.AddForce(new Vector2(0, jumpower));

    }
}

void FixedUpdate()
{
    isgrounded = Physics2D.OverlapCircle(checkslot.position, groundradius, LM);
    myanim.SetBool("groundedanim", isgrounded);
    myanim.SetFloat("verticalspeed", myrb.velocity.y);

    float move = Input.GetAxis ("Horizontal");
    RB.velocity = new Vector 2 (move * movespeed, RB.velocity.y);
}

标签: unity3d

解决方案


跳上斜坡时,您Physics2D.OverlapCircle与斜坡相交,从而使isgrounded=true,如果按下向上箭头,则使您施加更大的力。

解决方案可能是在跳跃之前检查您的角色是否已经向上移动

if (isgrounded && Input.GetKeyDown(KeyCode.UpArrow) && myrb.velocity.y <= 0.0f)

这样,如果您已经向上移动,您将不会再次跳跃。

也许您可以玩一个较小的groundradius和/或尝试checkslot.position以不以相同方式与斜坡相交的方式移动。


推荐阅读