I am using a runtime gizmo to translate gameobjects in my game. I need to know how to clamp the objects movement in an irregular shape.
For example: If the shape is a Box.
n calculate the Bounds of this shape and using the Bounds.max.x, Bounds.min.x, Bounds.max.y, Bounds.min.y, Bounds.max.z, Bounds.min.z and clamp the position like this:
Vector3 pos = gameObject.transform.position;
pos.x = Mathf.Clamp(pos.x,Bounds.min.x,Bounds.max.x);
pos.y = Mathf.Clamp(pos.y,Bounds.min.y,Bounds.max.y);
pos.x = Mathf.Clamp(pos.z,Bounds.min.z,Bounds.max.z);
gameObject.transform.position = pos;
This is all good till it's a box or a rectangular shape, because I can use either Renderer.Bounds or Collider.Bounds, However, if it is a irregular shape like this:
calculate Bounds value, it will be incorrect since Bounding box/bounds will always be calculated in a square/box shape and will give incorrect max and min value, making the gameObject go outside the shape.
I am using the below RuntimeGizmo to translate my gameObject. https://github.com/HiddenMonk/Unity3DRuntimeTransformGizmo
p.s. The Shape in my case is a square shape and a L-shape room, made by using primitive cubes acting as walls. gameObject in question is a piece of furniture.