using System.Collections.Generic; using UnityEngine; public class MovableFurniture : MonoBehaviour { private List children; private List outlines; private Rigidbody _rigidbody; private Bounds combinedBounds; private Vector3 centerOffset; // Start is called before the first frame update void Start() { _rigidbody = GetComponent(); children = new List(GetComponentsInChildren()); outlines = new List(); foreach (Transform child in children) { var part = child.gameObject.AddComponent(); part.parent = this; var outline = child.gameObject.AddComponent(); outlines.Add(outline); outline.OutlineMode = ObjectOutline.Mode.OutlineAll; outline.OutlineColor = Color.yellow; outline.OutlineWidth = 5f; outline.enabled = false; } CalculateBounds(); } // Update is called once per frame void Update() { } public void OnPointerClickUp() { if (_rigidbody != null) { _rigidbody.useGravity = true; } } public void OnPointerClickDown() { if (_rigidbody != null) { _rigidbody.useGravity = false; } } public void OnPointerEnter() { foreach (ObjectOutline outline in outlines) { /*child.OutlineMode = ObjectOutline.Mode.OutlineAll; child.OutlineColor = Color.yellow; child.OutlineWidth = 5f;*/ outline.enabled = true; } } public void OnPointerExit() { foreach (ObjectOutline outline in outlines) { outline.enabled = false; } } public void SnapTo(Vector3 position, LayerMask layerMask) { Vector3 displacement = Vector3.zero; this.transform.position = position + centerOffset; Vector3[] directions = { Vector3.right, Vector3.left, Vector3.up, Vector3.down, Vector3.forward, Vector3.back }; int[] extents = { 0, 0, 1, 1, 2, 2 }; for (int i = 0; i < 6; i++) { Ray raycast = new Ray(transform.position - centerOffset - (directions[i] * combinedBounds.extents[extents[i]]), (directions[i] * combinedBounds.size[extents[i]])); RaycastHit hit; bool SnapbHit = Physics.Raycast(raycast, out hit, combinedBounds.size[extents[i]], layerMask); if (SnapbHit) { displacement[i / 2] -= (i % 2 == 1 ? -1 : 1) * (combinedBounds.size[extents[i]] - (hit.distance)); } } /*Ray raycast = new Ray((transform.position - centerOffset) - (new Vector3(0, 0, combinedBounds.extents.z)), (new Vector3(0, 0, combinedBounds.size.z ))); RaycastHit hit; bool SnapbHit = Physics.Raycast(raycast, out hit, combinedBounds.size.z, layerMask); if (SnapbHit) { Debug.Log("Hit"); //displacement.z -= (0 % 2 == 1 ? -1 : 1) * (2*combinedBounds.size.z - (hit.distance)); displacement.z -= (combinedBounds.size.z - (hit.distance)); }*/ Debug.Log(displacement); this.transform.Translate(displacement, Space.World); } void OnDrawGizmos() { // Draw a yellow sphere at the transform's position Gizmos.color = Color.red; Gizmos.DrawWireCube(transform.position - centerOffset, combinedBounds.size); Gizmos.DrawRay((transform.position - centerOffset) - (new Vector3(0, 0, combinedBounds.extents.z)), (new Vector3(0, 0, combinedBounds.size.z))); } private void CalculateBounds() { if (GetComponent() != null) { combinedBounds = GetComponent().bounds; } else { combinedBounds = new Bounds(transform.position, Vector3.zero); } var renderers = GetComponentsInChildren(); foreach (Collider child in renderers) { if (child != GetComponent()) { if (combinedBounds == null) { combinedBounds = child.bounds; } else { combinedBounds.Encapsulate(child.bounds); } } } //combinedBounds = renderers[0].bounds; centerOffset = transform.position - combinedBounds.center; } }