void AttemptArPlaneMove(Vector3 arHitPos, HitNormal arHitnormal) { if (heldRb) // Revert to plane movement { heldRb.isKinematic = false; heldRb = null; } Vector3 offset = Vector3.zero; if (selectedChildInst != null) { offset = selectedInst.transform.position - selectedChildInst.transform.position; offset.y = 0f; } selectedInst.transform.position = arHitPos + offset; }
HitNormal GetArRaycastHitPosition(Vector2 touchPoint) { List <ARRaycastHit> hitsInfo = new List <ARRaycastHit>(); // Stores info about what was hit HitNormal hitNorm = new HitNormal(farAwayPos, Quaternion.identity, false); float maxDist = 25f; if (raycastManager.Raycast(touchPoint, hitsInfo, UnityEngine.XR.ARSubsystems.TrackableType.PlaneWithinBounds)) // Returns true if plane is hit { if (Vector3.Distance(hitsInfo[0].pose.position, Camera.main.transform.position) < maxDist) { hitNorm.hitPos = hitsInfo[0].pose.position; hitNorm.orientationOfNormal = hitsInfo[0].pose.rotation; hitNorm.hitValid = true; } } return(hitNorm); }
void Update() { //CheckPermissions(); if (debugDestroyDimensioner) { ClearInstances(); debugDestroyDimensioner = false; } #if UNITY_EDITOR if (debugPlace) { debugPlace = false; InstantiateDebug(); } #endif if (interactionsAllowed) { if (Input.touchCount == 2) { if (selectedInst) { if (scalerCrt == null && ArSettingsManager.instance.objectScalingAllowed) { Debug.Log("scale " + selectedInst.name); scalerCrt = StartCoroutine(ScaleObject(selectedInst)); mode = Mode.ScaleRotate; } if (rotateCrt == null && ArSettingsManager.instance.objectRotateAllowed) { Debug.Log("rot " + selectedInst.name); rotateCrt = StartCoroutine(RotateObject(selectedInst)); mode = Mode.ScaleRotate; } } } else if (Input.touchCount == 1) // Need to check that player is touching screen { bool emptySpace; // pointing that way Vector2 touchPoint = Input.GetTouch(0).position; // Position on the screen of phone (2 dimensional) HitNormal hitNorm = GetArRaycastHitPosition(touchPoint); Vector3 arHitPos = hitNorm.hitPos; if (Input.GetTouch(0).phase == TouchPhase.Began) { emptySpace = false; Debug.Log("Single touch registered "); if (mode != Mode.Moving) // *** Select or instantiate *** { Debug.Log("Not moving obj"); if (ArSettingsManager.instance.objectSelectionAllowed) { if (ObjectSelect(touchPoint)) // Attempt object selection { Debug.Log("Selected " + selectedInst.name); mode = Mode.Selected; } else if (!hitNorm.hitValid) { emptySpace = true; } } else { DeselectObject(); } if (hitNorm.hitValid) { emptySpace = false; if (mode == Mode.Unselected && prefabInstances.Count == 0 && ArSettingsManager.instance.prefabPlacementAllowed) { Debug.Log("Attempt start Inst method"); InstantiateAr(arHitPos, hitNorm.orientationOfNormal); } } if (emptySpace) { DeselectObject(); } } } else if (selectedInst != null && mode != Mode.ScaleRotate && ArSettingsManager.instance.objectMovementAllowed) // *** Move object *** { if (selectedInst.tag != "SelectableNoMove") { if (hitNorm.hitValid) { AttemptArPlaneMove(arHitPos, hitNorm); } else if (!heldRb) // looking in space { AttemptGrabMove(); } mode = Mode.Moving; StartCoroutine(FireActionTakenEvent(ArAction.PrefabMoved, selectedInst.gameObject)); if (source.clip != moveClip) { PlaySoundRandomPitch(moveClip, false); } } } } else if (mode == Mode.Moving || mode == Mode.ScaleRotate) // *** Done moving object *** { StopScaleRot(); } } }