This commit is contained in:
Axel Galand 2023-01-18 16:23:15 +01:00
parent 002e7278a1
commit 4b5b691e6b
2 changed files with 54 additions and 54 deletions

View File

@ -70,7 +70,7 @@ public class MovableFurniture : Furniture
} }
} }
/*public void SnapTo(Vector3 position, LayerMask layerMask, Vector3 direction) /*public void SnapTo(Vector3 position, LayerMask layerMask, Vector3 direction)
{ {
Vector3 displacement = Vector3.zero; Vector3 displacement = Vector3.zero;
this.transform.position = position + centerOffset; this.transform.position = position + centerOffset;
@ -91,62 +91,62 @@ public class MovableFurniture : Furniture
this.transform.Translate(displacement, Space.World); this.transform.Translate(displacement, Space.World);
}*/ }*/
public void SnapTo(Vector3 position, LayerMask layerMask, Vector3 direction) public void SnapTo(Vector3 position, LayerMask layerMask, Vector3 direction)
{ {
Vector3 previousPosition = transform.position; Vector3 previousPosition = transform.position;
Vector3 offset = Vector3.zero; Vector3 offset = Vector3.zero;
if (wallMount) if (wallMount)
{ {
transform.position = position; transform.position = position;
// check if object has objectBounds, if not combine the bounds of its children // check if object has objectBounds, if not combine the bounds of its children
Bounds objectBounds; Bounds objectBounds;
if (combinedBounds == null) if (combinedBounds == null)
{ {
objectBounds = new Bounds(); objectBounds = new Bounds();
foreach (Transform child in children) foreach (Transform child in children)
{ {
objectBounds.Encapsulate(child.GetComponent<Renderer>().bounds); objectBounds.Encapsulate(child.GetComponent<Renderer>().bounds);
} }
} }
else else
{ {
objectBounds = (Bounds)combinedBounds; objectBounds = (Bounds)combinedBounds;
} }
Vector3 objectSize = objectBounds.size; Vector3 objectSize = objectBounds.size;
offset = new Vector3(objectSize.x / 2, objectSize.y / 2, objectSize.z / 2); offset = new Vector3(objectSize.x / 2, objectSize.y / 2, objectSize.z / 2);
Collider[] colliders = Physics.OverlapBox(transform.position + offset, offset, transform.rotation, layerMask); Collider[] colliders = Physics.OverlapBox(transform.position + offset, offset, transform.rotation, layerMask);
if (colliders.Length > 0) if (colliders.Length > 0)
{ {
for (int i = 0; i < colliders.Length; i++) for (int i = 0; i < colliders.Length; i++)
{ {
offset += colliders[i].bounds.ClosestPoint(transform.position) - transform.position; offset += colliders[i].bounds.ClosestPoint(transform.position) - transform.position;
} }
offset /= colliders.Length; offset /= colliders.Length;
transform.position += offset; transform.position += offset;
} }
} }
else else
{ {
Vector3 newPosition = new Vector3(position.x, transform.position.y, position.z); Vector3 newPosition = new Vector3(position.x, transform.position.y, position.z);
transform.position = newPosition; transform.position = newPosition;
Bounds objectBounds = GetComponent<Collider>().bounds; Bounds objectBounds = GetComponent<Collider>().bounds;
Vector3 objectSize = objectBounds.size; Vector3 objectSize = objectBounds.size;
offset = new Vector3(objectSize.x / 2, objectSize.y / 2, objectSize.z / 2); offset = new Vector3(objectSize.x / 2, objectSize.y / 2, objectSize.z / 2);
Collider[] colliders = Physics.OverlapBox(transform.position + offset, offset, transform.rotation, layerMask); Collider[] colliders = Physics.OverlapBox(transform.position + offset, offset, transform.rotation, layerMask);
if (colliders.Length > 0) if (colliders.Length > 0)
{ {
for (int i = 0; i < colliders.Length; i++) for (int i = 0; i < colliders.Length; i++)
{ {
offset += colliders[i].bounds.ClosestPoint(transform.position) - transform.position; offset += colliders[i].bounds.ClosestPoint(transform.position) - transform.position;
} }
offset /= colliders.Length; offset /= colliders.Length;
transform.position += offset; transform.position += offset;
} }
} }
} }
void OnDrawGizmos() void OnDrawGizmos()
{ {
// Draw a yellow sphere at the transform's position // Draw a yellow sphere at the transform's position
Gizmos.color = Color.red; Gizmos.color = Color.red;