unity自用1 #实现接触地面时跳跃

#实现接触地面时跳跃

private bool isground;
[SerializeField] private float groundcheck;
[SerializeField] private LayerMask whatisground;
 private void OnDrawGizmos()
 {
     Gizmos.DrawLine(transform.position,new Vector3(transform.position.x,transform.position.y-groundcheck));
 }

OnDrawGizmos的DrawLine方法可以绘画一条线段

可以看到这条线的起始点为现有位置,终止点为向下距离为-groundcheck的点

这样可以显示在场景上,便于调整

update中输入:

 isground=Physics2D.Raycast(transform.position,Vector2.down,groundcheck,whatisground);
  • transform.position:表示射线的起点,通常是当前物体的位置。
  • Vector2.down:表示射线的方向,这里是向下的方向,即垂直向下。
  • groundcheck:表示射线的长度或检测距离,即射线会延伸多远进行碰撞检测。
  • whatisground:表示射线会与哪些层进行碰撞检测,只有在指定的层上的物体才会被检测到碰撞。

当线段与gound层(unity中指定gound为whatisground)接触时,isground为true,反之为false

 private void jump()
 {
    if(isground)
     rb.velocity = new Vector2(rb.velocity.x, jumpa);
 }

添加到jump方法中

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值