这是我玩家的跳转代码:
void Update()
{
float oldmoveHorizontal = moveHorizontal;
moveHorizontal = Joystick.Horizontal;
moveVertical = Joystick.Vertical;
if (isJumping == false && moveVertical >= .7f)
{
moveVertical = Speed * 70;
isJumping = true;
}
ball_move = new Vector2(moveHorizontal, moveVertical);
ball_rigid.AddForce(ball_move);
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
isJumping = false;
}
}
}
如果无法下载:https://ibb.co/yVgXmrM
答案 0 :(得分:1)
一种解决方案是检查碰撞的碰撞点的位置,并查看它们中的任何一个距离玩家中心“足够低”,以成为可跳过的碰撞:
private Collider2D playerCollider;
private float playerJumpableOffset = 0.001;
void Start() { playerCollider = GetComponent<Collider2D>(); }
void OnCollisionEnter2D(Collision2D col)
{
float jumpableWorldHeight = transform.position.y - playerJumpableOffset;
for (int i = 0 ; i < col.contactCount && isJumping; i++)
{
Vector2 contactPoint = col.GetContact(i).point;
if (contactPoint.y <= jumpableWorldHeight)
{
isJumping = false;
}
}
}
答案 1 :(得分:1)
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "Ground")
{
foreach (ContactPoint2D item in col.contacts)
{
Debug.Log("Normal:" + item.normal);
if (item.normal.y > 0 && col.gameObject.tag == "Ground")
{
isJumping = false;
Debug.Log("Top of Collider");
}
}
}}
我通过检查对撞机的顶部找到了解决方案。 通过这种编码方式,如果玩家触摸对撞机顶部,它将再次激活跳跃。