Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

OverlapBox not working

Discussion in 'Physics' started by CasaNu, Sep 4, 2022.

  1. CasaNu

    CasaNu

    Joined:
    Dec 21, 2021
    Posts:
    15
    I'm trying to detect all materials on an anvil for crafting with overlapbox (similar to codeMonkey's tutorial) but it just doesn't work. The overlap box does not detect any colliders. Here is my code:
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Anvil : MonoBehaviour
    6. {
    7.     public BoxCollider collider;
    8.     public LayerMask mask;
    9.     // Start is called before the first frame update
    10.     void Start()
    11.     {
    12.    
    13.     }
    14.  
    15.     // Update is called once per frame
    16.     void Update()
    17.     {
    18.         if(Input.GetButtonDown("Fire2"))
    19.         {
    20.             CraftWithCollider(collider);
    21.         }
    22.     }
    23.     void OnDrawGizmos()
    24.     {
    25.         Gizmos.color = Color.red;
    26.         Gizmos.matrix = transform.localToWorldMatrix;
    27.         Gizmos.DrawWireCube(collider.transform.position + (collider.transform.rotation * collider.center), Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale));
    28.     }
    29.     private void CraftWithCollider(BoxCollider collider)
    30.     {
    31.         Debug.Log("Getting...");
    32.         Collider[] colliders = Physics.OverlapBox(
    33.             center:collider.transform.position + (collider.transform.rotation * collider.center),
    34.             halfExtents:Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale),
    35.             orientation:collider.transform.rotation,
    36.             layerMask:mask
    37.         );
    38.         List<Transform> objectsInBox = new List<Transform>();
    39.         List<string> ObjectsName = new List<string>();
    40.         foreach (var c in colliders)
    41.         {
    42.             Transform t = c.transform;
    43.             if(t.GetComponent<InventoryMine>() != null && t != collider.transform && !objectsInBox.Contains(t))
    44.             {
    45.                 objectsInBox.Add(t);
    46.                 ObjectsName.Add(t.GetComponent<InventoryMine>().Me);
    47.             }
    48.         }
    49.         for (int i = 0; i < objectsInBox.Count; i++)
    50.         {
    51.             Debug.Log(objectsInBox[i].GetComponent<InventoryMine>().Me);
    52.         }
    53.     }
    54. }
    I have tried using OnGizmosDraw to draw a wire cube where it should be overlapping, but I don't understand matrix.
    Any advice or suggestions would be appreciated.
     
    Last edited: Sep 4, 2022
  2. RoyalPrince12

    RoyalPrince12

    Joined:
    Mar 25, 2022
    Posts:
    1
    Maybe enable gizmos and see if the box is actually colliding with the rock?
     
  3. lightbug14

    lightbug14

    Joined:
    Feb 3, 2018
    Posts:
    446
    First check how many colliders are being detected. For example, you could print the length of the array:
    Code (CSharp):
    1. private void CraftWithCollider(BoxCollider collider)
    2. {
    3.     Debug.Log("Getting...");
    4.     Collider[] colliders = Physics.OverlapBox(
    5.         center: collider.transform.position + (collider.transform.rotation * collider.center),
    6.         halfExtents: Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale),
    7.         orientation: collider.transform.rotation,
    8.         layerMask: mask
    9.     );
    10.  
    11.     print($"{colliders.Length} colliders detected");
    12.  
    13.     // ...
    14. }
    That version of OverlapBox (the "alloc" version) is not recommended if you need to call it all the time. Here's the "non-alloc" version:
    Code (CSharp):
    1. Collider[] colliders = new Collider[20];
    2.  
    3. private void CraftWithCollider(BoxCollider collider)
    4. {
    5.     Debug.Log("Getting...");
    6.     int count = Physics.OverlapBoxNonAlloc(
    7.         center: collider.transform.position + (collider.transform.rotation * collider.center),
    8.         halfExtents: Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale),
    9.         results: colliders,
    10.         orientation: collider.transform.rotation,
    11.         layerMask: mask
    12.     );
    13.  
    14.     print($"{count} colliders detected");
    15.  
    16.     // ...
    17. }
     
  4. CasaNu

    CasaNu

    Joined:
    Dec 21, 2021
    Posts:
    15
    I will try this soon
     
  5. CasaNu

    CasaNu

    Joined:
    Dec 21, 2021
    Posts:
    15
    It worked!!!!! Thank you very much!
     
  6. CasaNu

    CasaNu

    Joined:
    Dec 21, 2021
    Posts:
    15
    but it only worked on sphere collider not my box collider, any idea why?