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:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Anvil : MonoBehaviour
{
public BoxCollider collider;
public LayerMask mask;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
if(Input.GetButtonDown("Fire2"))
{
CraftWithCollider(collider);
}
}
void OnDrawGizmos()
{
Gizmos.color = Color.red;
Gizmos.matrix = transform.localToWorldMatrix;
Gizmos.DrawWireCube(collider.transform.position + (collider.transform.rotation * collider.center), Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale));
}
private void CraftWithCollider(BoxCollider collider)
{
Debug.Log("Getting...");
Collider[] colliders = Physics.OverlapBox(
center:collider.transform.position + (collider.transform.rotation * collider.center),
halfExtents:Vector3.Scale(collider.size * 0.5f, collider.transform.lossyScale),
orientation:collider.transform.rotation,
layerMask:mask
);
List<Transform> objectsInBox = new List<Transform>();
List<string> ObjectsName = new List<string>();
foreach (var c in colliders)
{
Transform t = c.transform;
if(t.GetComponent<InventoryMine>() != null && t != collider.transform && !objectsInBox.Contains(t))
{
objectsInBox.Add(t);
ObjectsName.Add(t.GetComponent<InventoryMine>().Me);
}
}
for (int i = 0; i < objectsInBox.Count; i++)
{
Debug.Log(objectsInBox[i].GetComponent<InventoryMine>().Me);
}
}
}
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.