Physics.OverlapSphere layerMask位操作

最近写个 球形射线探测的例子 里面用到 射线,层,位操作,翻阅资料才震荡layer里面有32个 刚好是32位,可以是手雷,别的之类的


     void ExplosionDamage( Vector3 center, float radius)
    {
         //表示位操作, 只这一句实际上表示射线查询只在Ground所在这个层级查找。 32位 代表32层
         Collider[] hitColliders = Physics. OverlapSphere( center, radius, 1<< LayerMask. NameToLayer( "Enemy"));
         int i = 0;
         while ( i < hitColliders. Length)
        {
             Vector3 Dir = hitColliders[ i]. transform. position - transform. position;
             Debug. DrawRay( transform. position, Dir, Color. blue, 1f);

             if( hitColliders[ i]. transform. CompareTag( "Enemy"))
            {
                 hitColliders[ i]. transform. GetComponent< Enemy>(). Death();
            
            }
                 i++;
             Debug. Log( hitColliders[ i]. transform. name);  
             //被扫到的碰撞体的行为,例如:
             //hitColliders[i].GetComponent().material.color = Color.red;
        
        }
    }
关于位操作符,下面博主贴出了这个
https://www.cnblogs.com/chenxizhang/archive/2008/10/20/1315066.html
 public static void Main() 
  { 
    int a=6&3; 
    Console.WriteLine("a={0}",a); 
    //6的二进制是00000110,3的二进制是00000011,按位与后等于00000010,  即2。
    int b=6|3; 
    Console.WriteLine("b={0}",b); 
    //6的二进制是00000110,3的二进制是00000011,按位或后等于00000111,即7 
    int c=~6; 
    Console.WriteLine("c={0}",c); 
    //6的二进制是00000110,按位取反后是11111001即-7 
    int d=6^3; 
    Console.WriteLine("d={0}",d); 
    //6的二进制是00000110,3的二进制是00000011,按位异或后等于00000101,即5 
    int e=6<<3; 
    Console.WriteLine("e={0}",e); 
    //6的二进制是00000110,左移三位后等于00101000,即48 
    int f=6>>2; 
    Console.WriteLine("f={0}",f); 
    //6的二进制是00000110,右移二位等于00000001,即1 
  } 

你可能感兴趣的:(S3_Unity学习,虚拟现实开发)