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