private Hit_Data Position_Check(Vector3 i_dir, float i_dist) { RaycastHit hit; Physics.Raycast(transform.position, i_dir, out hit, i_dist); Vector3 hit_pos; bool is_object_present = false; Hit_Data return_hit = new Hit_Data(); if (hit.collider != null) { is_object_present = true; hit_pos = hit.collider.transform.position; return_hit.hit_tag = hit.collider.tag; return_hit.hit_object = hit.collider.gameObject; } else { hit_pos = transform.position + (i_dir * i_dist); return_hit.hit_tag = ""; } Color ray_colour; if (is_object_present) { ray_colour = Color.green; } else { ray_colour = Color.white; } Debug.DrawLine(transform.position, hit_pos, ray_colour); return_hit.is_hit = is_object_present; return_hit.hit_point = hit_pos; return(return_hit); }
// Update is called once per frame void Update() { seen_pos_list.Clear(); seen_interactables.Clear(); Vector3 starting_dir = Quaternion.AngleAxis(-(float)((NUM_OF_ANGLES / 2) * angle_size), Vector3.up) * transform.forward; // Recursively searches for an object in the AI's define FOV for (int i = 0; i < NUM_OF_ANGLES; i++) { for (int j = 0; j < max_look_dist * 5; j++) { Hit_Data temp_hit = Position_Check(starting_dir, (float)(j / 5.0f)); Vector3Int rounded_hit_pos = new Vector3Int(); rounded_hit_pos.x = Mathf.RoundToInt(temp_hit.hit_point.x); rounded_hit_pos.y = 0; rounded_hit_pos.z = Mathf.RoundToInt(temp_hit.hit_point.z); if (temp_hit.is_hit) { j = max_look_dist * 5; seen_pos_list.Add(rounded_hit_pos); if ((temp_hit.hit_tag == "Copper") || (temp_hit.hit_tag == "Red")) { // Adds the seen object to the list of seen objects seen_interactables.Add(temp_hit.hit_object.GetComponent <Block_Interactable>()); } } else { seen_pos_list.Add(rounded_hit_pos); } // If the object exists it will be added to the memory banks for this AI Add_Data_To_Memory(rounded_hit_pos, temp_hit.hit_tag); } starting_dir = Quaternion.AngleAxis(angle_size, Vector3.up) * starting_dir; } }