I want to check if a 2d player icon in within the bounds of a 2d map
I have tried it before I put the post up but it doesn’t seem to work, any idea why?
this is the code:
if (GetComponentInParent<BoxCollider>().bounds.Contains(transform.position))
{
Debug.Log("set parent to icon inventory");
parentAfterDrag = iconInv.transform;
}
bump
You mention 2D twice but are using a 3d Box Collider? It may not be making a difference but it does seem off.
In any case you want to be debugging the values you have to see if they would actually create a true result. Note what the docs says about negative extends always returning a false value, too.
What do you consider a 2d map? If it’s 2d, why is your sample code using a 3d BoxCollider? Is it’s pseudo 2d / 2.5d?
Note that “Bounds” are an axis aligned bounding box and does not necessarily represent the actual area / volume depending on the rotation of the object. If the object is aligned with the world axis, it should work pretty well. Give us some details about your case and not just bump your thread without giving more information. Your thread is only a single sentence. Explain what you’re actually doing and using. Do you use 2d or 3d? If it’s 3d, what is the orientation of that 2d plane you talk about (x-y, x-z, y-z?, some other rotated orientation?). Maybe show some screenshots or drawing that supports explaining your case. What components or the Unity engine are you using to represent your player and your map?
You really have to work on presenting your question(s). You can not really be upset if you just get a single link as an answer when your question is equally vague.
ok, you are right I just didn’t really know if any others info matters but here you go:
This is the object I want to check for his bounds
this is the object I want to check if he is in said bounds:
this is the code that is supposed to be checking if the player is in bounds, the script is in the player icon:
if (GetComponentInParent<BoxCollider>().bounds.Contains(transform.position))
{
Debug.Log("set parent to icon inventory");
parentAfterDrag = iconInv.transform;
}
and this is the hierarchy:
on drag player icon moves 1 up in hierarchy and becoming the child of Map grid(which is the object that I want to check if player icon is in bounds of)
this currently doesn’t work, if I add an “!” before the condition of the if it sets parent to the icon inventory all the time
I have tried it with both but it still doesn’t seem to work.
UI and colliders are not really a thing, so going down the route of colliders and Bounds is probably a bum steer. I would not be using them together in any case.
If we’re talking UI here, then you maybe want to look at Rect.Contains: Unity - Scripting API: Rect.Contains
RectTransform, as the name would imply, has a Rect property: Unity - Scripting API: RectTransform.rect
Note it gives a local Rect, so factor that in when doing your calculations. It might not be a one-line affair, just be aware.
what do you mean by local rect?
The values of the rect are local to it’s parent. As in Local Space, as opposed to World Space.
it works but only on the leftmost 5x6 slots any idea why?
this is the map and the area marked is the area it works in
This is my code now
if (!parentOnBeginDrag.GetComponent<RectTransform>().rect.Contains(rt.position, true))
{
Debug.Log("set parent to icon inventory");
parentAfterDrag = iconInv.transform;
}
I don’t know why, but I know how you can find out: debug it on your end.
It now works, I just needed to add localPosition instead on position thanks