Пример #1
0
        //Check if ground is valid layer, this one check only the first hit collision (more strict)
        public bool CheckValidFloor()
        {
            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.RaycastCollision(p0, dir, out h0);
            bool       f1 = PhysicsTool.RaycastCollision(p1, dir, out h1);
            bool       f2 = PhysicsTool.RaycastCollision(p2, dir, out h2);
            bool       f3 = PhysicsTool.RaycastCollision(p3, dir, out h3);
            bool       f4 = PhysicsTool.RaycastCollision(p4, dir, out h4);

            f0 = f0 && PhysicsTool.IsLayerIsInLayerMask(h0.collider.gameObject.layer, floor_layer);
            f1 = f1 && PhysicsTool.IsLayerIsInLayerMask(h1.collider.gameObject.layer, floor_layer);
            f2 = f2 && PhysicsTool.IsLayerIsInLayerMask(h2.collider.gameObject.layer, floor_layer);
            f3 = f3 && PhysicsTool.IsLayerIsInLayerMask(h3.collider.gameObject.layer, floor_layer);
            f4 = f4 && PhysicsTool.IsLayerIsInLayerMask(h4.collider.gameObject.layer, floor_layer);

            if (build_flat_floor)
            {
                return(f1 && f2 && f3 && f4 && f0); //Floor must be valid on all sides
            }
            else
            {
                return(f1 || f2 || f3 || f4 || f0); //Floor must be valid only on one side
            }
        }
        //Detect if there is an obstacle in front of the character
        private void DetectFronted()
        {
            float radius = destruct.hit_range * 2f;

            Vector3 center = destruct.GetCenter();
            Vector3 dir    = move_target_avoid - transform.position;
            Vector3 dirl   = Quaternion.AngleAxis(-45f, Vector3.up) * dir.normalized;
            Vector3 dirr   = Quaternion.AngleAxis(45f, Vector3.up) * dir.normalized;

            RaycastHit h, hl, hr;
            bool       fc = PhysicsTool.RaycastCollision(center, dir.normalized * radius, out h);
            bool       fl = PhysicsTool.RaycastCollision(center, dirl.normalized * radius, out hl);
            bool       fr = PhysicsTool.RaycastCollision(center, dirr.normalized * radius, out hr);

            is_fronted_center = fc && (target == null || h.collider.gameObject != target);
            is_fronted_left   = fl && (target == null || hl.collider.gameObject != target);
            is_fronted_right  = fr && (target == null || hr.collider.gameObject != target);

            int front_count = (fc ? 1 : 0) + (fl ? 1 : 0) + (fr ? 1 : 0);

            front_dist = (fc ? h.distance : 0f) + (fl ? hl.distance : 0f) + (fr ? hr.distance : 0f);
            if (front_count > 0)
            {
                front_dist = front_dist / (float)front_count;
            }

            is_fronted = is_fronted_center || is_fronted_left || is_fronted_right;
        }
        //Detect if there is an obstacle in front of the character
        private void DetectFronted()
        {
            Vector3 scale   = transform.lossyScale;
            float   hradius = collide.height * scale.y * 0.5f - 0.02f; //radius is half the height minus offset
            float   radius  = collide.radius * (scale.x + scale.y) * 0.5f + 0.5f;

            Vector3 center = GetColliderCenter();
            Vector3 p1     = center;
            Vector3 p2     = center + Vector3.up * hradius;
            Vector3 p3     = center + Vector3.down * hradius;

            RaycastHit h1, h2, h3;
            bool       f1 = PhysicsTool.RaycastCollision(p1, facing * radius, out h1);
            bool       f2 = PhysicsTool.RaycastCollision(p2, facing * radius, out h2);
            bool       f3 = PhysicsTool.RaycastCollision(p3, facing * radius, out h3);

            is_fronted = f1 || f2 || f3;

            //Debug.DrawRay(p1, facing * radius);
            //Debug.DrawRay(p2, facing * radius);
            //Debug.DrawRay(p3, facing * radius);
        }
Пример #4
0
        //Make sure there is no obstacles in between the player and the building, this applies only if placing with keyboard/gamepad
        public bool CheckIfAccessible()
        {
            PlayerControls controls = PlayerControls.Get(building_character.player_id);
            bool           game_pad = controls != null && controls.IsGamePad();

            if (position_set || !game_pad)
            {
                return(true); //Dont check this is placing with mouse or if position already set
            }
            if (building_character != null)
            {
                Vector3 center       = building_character.GetColliderCenter();
                Vector3 build_center = transform.position + Vector3.up * build_ground_dist;
                Vector3 dir          = build_center - center;

                RaycastHit h1;
                bool       f1 = PhysicsTool.RaycastCollision(center, dir, out h1);

                return(!f1 || h1.collider.GetComponentInParent <Buildable>() == this);
            }
            return(false);
        }
Пример #5
0
        //Check if there is a flat floor underneath (can't build a steep cliff)
        public bool CheckIfFlatGround()
        {
            if (!build_flat_floor)
            {
                return(true); //Dont check for flat ground
            }
            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.RaycastCollision(p0, dir, out h0);
            bool       f1 = PhysicsTool.RaycastCollision(p1, dir, out h1);
            bool       f2 = PhysicsTool.RaycastCollision(p2, dir, out h2);
            bool       f3 = PhysicsTool.RaycastCollision(p3, dir, out h3);
            bool       f4 = PhysicsTool.RaycastCollision(p4, dir, out h4);

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