- Home /
Question by
Raimi · Dec 03, 2017 at 10:26 PM ·
uiui imageboxcolliderworldspacescreenspace
World BoxCollider size to Match Screen Space UI image size
I have a Screen SpaceOverlay canvas with a UI image of a box. I also have a world space boxcollider.
I need the boxcolliders width and height to match the UI image width and height.
Tried everything, but no joy.
Any help please?
Comment
Best Answer
Answer by Raimi · Dec 04, 2017 at 12:46 AM
I managed to get it working. Here is what I did...
Im drawing a drag selection box using UI image and touch input. Instead of using the values from the UI image I just used the touch input directly. (I use my own custom Touch Input class)
selectionCollider.GetComponent<BoxCollider>().size = new Vector3(
Mathf.Abs(Camera.main.ScreenToWorldPoint(td.GetTouch(0).position).x - Camera.main.ScreenToWorldPoint(touchStartPos).x),
30,
Mathf.Abs(Camera.main.ScreenToWorldPoint(td.GetTouch(0).position).z - Camera.main.ScreenToWorldPoint(touchStartPos).z));
this creates a BoxCollider that matches the dynamically resized screenspace UI image.