详解Unity地面检测方案

本文主要介绍了Unity地面检测方案,感兴趣的同学,可以参考下,并且亲自实验一下。

5.LayerMask设置方法

最后提下这个LayerMask

假设ground层为10,指定碰撞第10层Layer,写法为:Layermask mask=1<<10

但是,投射的胶囊体也会检测自己本身,如果你希望游戏中基本上任何能碰撞物体都能够用来站脚,那么应设置为:碰撞除了角色所在的Layer以外的所有层(假设Player层为8

写法为:~(1<<8)


bool OnGround() {
        pointBottom = transform.position + transform.up * radius-transform.up*overLapCapsuleOffset;
        pointTop = transform.position + transform.up * capsuleCollider.height - transform.up * radius;
        LayerMask ignoreMask = ~(1 << 8);
 
        colliders = Physics.OverlapCapsule(pointBottom, pointTop, radius, ignoreMask);
        Debug.DrawLine(pointBottom, pointTop,Color.green);
        if (colliders.Length!=0)
        {
            isOnGround = true;
            return true;
        }
        else
        {
             isOnGround = false;
            return false;
        }
}

以上就是详解Unity地面检测方案的详细内容,更多关于Unity地面检测方案的资料请关注得得之家其它相关文章!

本文标题为:详解Unity地面检测方案