事件处理程序的注册和检测是否在地面(防止多次跳跃)
事件处理程序的注册
input输入相关
PlayerIn.keyboard.jump.started += Jump; //生成跳跃方法
private void Jump(InputAction.CallbackContext context)
{
//throw new NotImplementedException();
if (PPc.isGround)
{
rb2.AddForce(transform.up * jumpForce, ForceMode2D.Impulse);
}
} //可自动生成
检测物体是否在地面
public class PayerPhysicsCheck : MonoBehaviour
{
public float rayLong = 0.1f;
public LayerMask groundLayer;
public bool isGround;
public CapsuleCollider2D col;
private void FixedUpdate()
{
Vector2 rayOrigin = col.bounds.center; //获取碰撞体中心
rayOrigin.y -= col.bounds.extents.y; //将起始点移到底部
RaycastHit2D hit = Physics2D.Raycast(rayOrigin, Vector2.down, rayLong, groundLayer); //返回一个RaycastHit2D类型的结构体,包含射线与物体相交的信息,如相交点、碰撞法线等
isGround = hit.collider != null;
Debug.DrawRay(rayOrigin, Vector2.down * rayLong, Color.red); //画出这条射线
}
}
浙公网安备 33010602011771号