float RaycastSnapCheck(Vector3 Start, Vector3 Direction, ref RaycastHit Hit) { RaycastHit[] hits = Physics.RaycastAll(Start, Direction); float closestDistance = int.MaxValue; //Find closest for (int i = 0; i < hits.Length; i++) { BuilderBlock1 theBuilderBlock = hits[i].collider.GetComponent <BuilderBlock1>(); if (theBuilderBlock != null) { if (theBuilderBlock.IsPlaced) { float distance = Vector3.Distance(Start, hits[i].point); if (closestDistance > distance) { closestDistance = distance; Hit = hits[i]; } } } } return(closestDistance); }
void Select() { Ray mouseRay = Camera.main.ScreenPointToRay(Input.mousePosition); RaycastHit[] mouseHits = Physics.RaycastAll(mouseRay); //Loop through each mouse hit for (int i = 0; i < mouseHits.Length; i++) { BuilderBlock1 theBuilderBlock = mouseHits[i].collider.GetComponent <BuilderBlock1>(); BuilderIgnoreSelection theBuilderIgnoreSelection = mouseHits[i].collider.GetComponent <BuilderIgnoreSelection>(); if (theBuilderIgnoreSelection == null && theBuilderBlock != null) { SelectedObject = mouseHits[i].collider.gameObject; SelectedObject.GetComponent <Rigidbody>().isKinematic = false; SelectedObject.GetComponent <Rigidbody>().constraints = RigidbodyConstraints.FreezeRotationX | RigidbodyConstraints.FreezeRotationY | RigidbodyConstraints.FreezeRotationZ | RigidbodyConstraints.FreezePositionY; SelectedObject.GetComponent <Rigidbody>().velocity = Vector3.zero; FixRotation(); SelectedObject.GetComponent <BuilderBlock1>().Detatch(); break; } } }