I’m building a simple drag and drop inventory using 2 type of cubes: Dragables and Slots. I want to get a reference of the Slot when I let go of a Dragable on top of it. This is the code that I can’t get to work properly:
RaycastHit hitInfo = new RaycastHit();
bool hit = Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hitInfo, 1 << LayerMask.NameToLayer("Slots"));
if (hit)
{
Debug.Log(hitInfo.collider.name);
}
The layer mask doesn’t seem to be working because it returns the first collider on all layers (in this case the Dragable) instead of only triggering on the Slots.
Here is my work around, which works great, but it seems like I shouldn’t need this much code?
RaycastHit[] hits;
hits = Physics.RaycastAll(Camera.main.ScreenPointToRay(Input.mousePosition));
int i = 0;
while (i < hits.Length) {
RaycastHit hit = hits*;*
- if( hit.collider.tag == “Slot” ){*
-
inSlot = true;*
-
Slot = hit.collider.transform;*
-
transform.position = new Vector3(hit.collider.transform.position.x, hit.collider.transform.position.y, -5f);*
-
return;*
- }*
- i++;*
}