1

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.

I can 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:

And, I 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.

Mohit Pundir
  • 109
  • 1
  • 7
  • why dont you use the built in physics and just make the walls have colliders to prevent the objects from going outside – Daahrien Jun 05 '17 at 10:40
  • I am already doing that, however it is not that effective since I am using a runtime Transform Gizmo to move objects. Transform gizmo is using Transform.translate which ignores physics all together, – Mohit Pundir Jun 06 '17 at 04:57

0 Answers0