using System.Collections.Generic; using UnityEngine; public class MovableFurniture : Furniture { private List children; private List outlines; private Rigidbody _rigidbody; public bool wallMount; private bool isColliding = false; private bool isGrabbing = false; // Start is called before the first frame update new void Start() { base.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(); if (!outline) continue; outlines.Add(outline); outline.OutlineMode = ObjectOutline.Mode.OutlineVisible; outline.OutlineColor = Color.yellow; outline.OutlineWidth = 5f; outline.enabled = false; } } // Update is called once per frame void Update() { } public void OnPointerClickUp() { isGrabbing = false; foreach (ObjectOutline outline in outlines) { outline.enabled = false; } if (_rigidbody != null) { _rigidbody.useGravity = true; _rigidbody.isKinematic = false; } } public void OnPointerClickDown() { isGrabbing = true; foreach (ObjectOutline outline in outlines) { outline.OutlineMode = ObjectOutline.Mode.OutlineAll; outline.OutlineColor = Color.blue; outline.OutlineWidth = 5f; outline.enabled = true; } if (_rigidbody != null) { _rigidbody.useGravity = false; _rigidbody.isKinematic = true; } } public void OnPointerEnter() { foreach (ObjectOutline outline in outlines) { outline.OutlineMode = ObjectOutline.Mode.OutlineAll; outline.OutlineColor = Color.yellow; outline.OutlineWidth = 5f; outline.enabled = true; } } public void OnPointerExit() { if (isGrabbing) { return; } foreach (ObjectOutline outline in outlines) { outline.enabled = false; } } /*public void SnapTo(Vector3 position, LayerMask layerMask, Vector3 direction) { 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)); } } this.transform.Translate(displacement, Space.World); }*/ public void SnapTo(Vector3 position, LayerMask layerMask, Vector3 direction) { Vector3 previousPosition = transform.position; Vector3 offset = Vector3.zero; //if (isColliding) { return; } transform.position = position; return; if (wallMount) { transform.position = position; Bounds objectBounds; if (combinedBounds == null) { objectBounds = new Bounds(); foreach (Transform child in children) { objectBounds.Encapsulate(child.GetComponent().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().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() { // 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 OnDisable() { if (outlines != null && outlines.Count > 0) { foreach (ObjectOutline outline in outlines) { outline.enabled = false; } } } //Check if collision before moving an object public void OnFurnitureCollisionEnter(Collider other) { /*foreach (ObjectOutline outline in outlines) { outline.OutlineColor = Color.red; outline.enabled = true; } isColliding = true;*/ } public void OnFurnitureCollisionStay(Collider other) { //isColliding = true; } public void OnFurnitureCollisionExit(Collider other) { //Run CheckFurnitureCollisions on next FixedUpdate loop. If no object is truly colliding, isColliding should be false inside CheckFurnitureCollisions /*Invoke("CheckFurnitureCollisions", Time.fixedTime * 2); isColliding = false;*/ } private void CheckFurnitureCollisions() { if (isColliding == true) return; foreach (ObjectOutline outline in outlines) { outline.OutlineColor = Color.yellow; outline.enabled = false; } } }