我只想在他脚踏实地的时候才能让角色跳起来。我不希望他能够“空中跳跃”,所以我带来了这个解决方案:
if (JumpButtonPressed()) {
if (GetComponent<BoxCollider2D>().IsTouchingLayers(LayerMask.NameToLayer("Ground"))) {
velocity.y = jumpForce;
}
}
这个想法是,只有当与“地面”层接触时,它才能跳跃。但这就是发生的事情:
它不起作用。如果他在侧面触摸平台,它也可以跳跃。我该怎么办?
答案 0 :(得分:0)
将circle overlap与位置矢量放在播放器的底部。从那里检测图层。
如果要编写类似于抓取,跳转等功能,则可能需要使用不同的平台层。
答案 1 :(得分:0)
创建一个bool isJumping并在空中设置为true。当它到达地面时 - 将其设置为假。如果你的代码片段在Update()中,那就像那样:
bool isJumping = false;
if (JumpButtonPressed() && !isJumping) {
if (GetComponent<BoxCollider2D>().IsTouchingLayers(LayerMask.NameToLayer("Ground"))) {
velocity.y = jumpForce;
isJumping = true;
}
}
if (GetComponent<BoxCollider2D>().IsTouchingLayers(LayerMask.NameToLayer("Ground"))) {
isJumping = false;
}