Example #1
0
        //Find the height to build
        public Vector3 FindBuildPosition(Vector3 pos, LayerMask mask)
        {
            float   offset = build_distance;
            Vector3 center = pos + Vector3.up * offset;
            Vector3 p0     = center;
            Vector3 p1     = center + Vector3.right * build_obstacle_radius;
            Vector3 p2     = center + Vector3.left * build_obstacle_radius;
            Vector3 p3     = center + Vector3.forward * build_obstacle_radius;
            Vector3 p4     = center + Vector3.back * build_obstacle_radius;
            Vector3 dir    = Vector3.down * (offset + build_ground_dist);

            RaycastHit h0, h1, h2, h3, h4;
            bool       f0 = PhysicsTool.RaycastCollisionLayer(p0, dir, mask, out h0);
            bool       f1 = PhysicsTool.RaycastCollisionLayer(p1, dir, mask, out h1);
            bool       f2 = PhysicsTool.RaycastCollisionLayer(p2, dir, mask, out h2);
            bool       f3 = PhysicsTool.RaycastCollisionLayer(p3, dir, mask, out h3);
            bool       f4 = PhysicsTool.RaycastCollisionLayer(p4, dir, mask, out h4);

            Vector3 dist_dir = Vector3.down * build_distance;

            if (f0 && h0.distance < dist_dir.magnitude)
            {
                dist_dir = Vector3.down * h0.distance;
            }
            if (f1 && h1.distance < dist_dir.magnitude)
            {
                dist_dir = Vector3.down * h1.distance;
            }
            if (f2 && h2.distance < dist_dir.magnitude)
            {
                dist_dir = Vector3.down * h2.distance;
            }
            if (f3 && h3.distance < dist_dir.magnitude)
            {
                dist_dir = Vector3.down * h3.distance;
            }
            if (f4 && h4.distance < dist_dir.magnitude)
            {
                dist_dir = Vector3.down * h4.distance;
            }

            return(center + dist_dir);
        }
Example #2
0
        //Check if its still valid floor after built, this one ignore itself and check only the layer (less strict)
        public bool CheckValidFloorBuilt()
        {
            Vector3 center = transform.position + Vector3.up * build_ground_dist;
            Vector3 p0     = center;
            Vector3 p1     = center + Vector3.right * build_obstacle_radius;
            Vector3 p2     = center + Vector3.left * build_obstacle_radius;
            Vector3 p3     = center + Vector3.forward * build_obstacle_radius;
            Vector3 p4     = center + Vector3.back * build_obstacle_radius;
            Vector3 dir    = Vector3.down * (build_ground_dist + build_ground_dist);

            RaycastHit h0, h1, h2, h3, h4;
            bool       f0 = PhysicsTool.RaycastCollisionLayer(p0, dir, floor_layer, out h0);
            bool       f1 = PhysicsTool.RaycastCollisionLayer(p1, dir, floor_layer, out h1);
            bool       f2 = PhysicsTool.RaycastCollisionLayer(p2, dir, floor_layer, out h2);
            bool       f3 = PhysicsTool.RaycastCollisionLayer(p3, dir, floor_layer, out h3);
            bool       f4 = PhysicsTool.RaycastCollisionLayer(p4, dir, floor_layer, out h4);

            return(f1 || f2 || f3 || f4 || f0);
        }