private void Update() { if (!TryGetTouchPosition(out Vector2 touchPosition) || IsObjectPlaced || _placedObject == null) { return; } if (arRaycastManager.Raycast(touchPosition, hits, TrackableType.PlaneWithinPolygon)) { var hitPose = hits[0].pose; GameObject instantiatedObject = Instantiate(_placedObject, hitPose.position, hitPose.rotation); instantiatedObject.SetActive(true); instantiatedObject.AddComponent <ContentScaler>(); IsObjectPlaced = true; ARPlane hitPlane = arPlaneManager.GetPlane(hits[0].trackableId); if (hitPlane != null) { Debug.Log("Plane is found"); ARAnchor anchor = arAnchorManager.AttachAnchor(hitPlane, hitPose); Debug.Log("Anchor is attached to plane : " + anchor.name); anchor.transform.SetParent(instantiatedObject.transform); } StopPlaneDetection(); } }
/// <summary> /// Immediately creates local anchor after detected marker intersects detected plane beneath it. /// Cloud anchor is created afterwards, but it takes some time. When it is finished, scene will be attached to it. /// Called if user clicks on the calibration cube displayed over detected marker. /// </summary> /// <param name="tf"></param> public void CreateAnchor(Transform tf) { #if (UNITY_ANDROID || UNITY_IOS) && !UNITY_EDITOR // try to raycast straight down to intersect closest plane List <ARRaycastHit> raycastHits = new List <ARRaycastHit>(); if (ARRaycastManager.Raycast(new Ray(tf.position, Vector3.down), raycastHits, TrackableType.PlaneWithinPolygon)) { // remove all old local anchors, if there are some (in case we are recalibrating) RemoveLocalWorldAnchor(); RemoveCloudWorldAnchor(); Pose hitPose = raycastHits[0].pose; TrackableId hitPlaneId = raycastHits[0].trackableId; ARPlane plane = ARPlaneManager.GetPlane(hitPlaneId); // set temporary world anchor WorldAnchorLocal = ARAnchorManager.AttachAnchor(plane, new Pose(hitPose.position, Quaternion.FromToRotation(tf.up, plane.normal) * tf.rotation)); // immediately attach scene to local anchor (after cloud anchor is created, scene will be attached to it) AttachScene(WorldAnchorLocal.gameObject); // Create cloud anchor if (Settings.Instance.UseCloudAnchors) { WorldAnchorCloud = ARAnchorManager.HostCloudAnchor(WorldAnchorLocal); StartCoroutine(HostCloudAnchor()); } else { Calibrated = true; worldAnchorVis = null; ActivateCalibrationElements(ControlBoxManager.Instance.CalibrationElementsToggle.isOn); } GameManager.Instance.Scene.SetActive(true); ActivateTrackableMarkers(false); } // if there is no plane beneath detected marker then display notification about unsufficient tracking else { Notifications.Instance.ShowNotification("Calibration error", "Plane beneath calibration marker is not detected"); //Play animation for moving with the device TrackingLostAnimation.PlayVideo(5f); } #endif }
public void Launch() { ARPlane arPlane = FindObjectOfType <ARPlane>(); Pose hitPose; var spawnPoint = arPlane.center + new Vector3(Random.Range(-arPlane.extents.x, arPlane.extents.x), 0, Random.Range(-arPlane.extents.y, arPlane.extents.y)); hitPose.position = spawnPoint; hitPose.rotation = Quaternion.Euler(0, Random.Range(-180, 180), 0); anchorManager.anchorPrefab = coolerPenguinPrefab; anchorManager.AttachAnchor(arPlane, hitPose); anchorManager.anchorPrefab = penguinPrefab; arPlaneManager.requestedDetectionMode = PlaneDetectionMode.None; //FindObjectOfType<ARPointCloud>().enabled = false; panelStart.SetActive(false); panelGenerating.SetActive(false); panelGame.SetActive(true); panelEnd.SetActive(false); currentState = GameState.PLAY; }
public void SpawnCharacters() { ARAnchor anchor = null; Debug.LogWarning("Charlie ARPlane alignement: " + arPlane.alignment); Debug.LogWarning("Charlie ARPlane: " + arPlane.center + arPlane.extents); if (arPlane.alignment == PlaneAlignment.HorizontalUp) { Pose hitPose; for (var i = anchors.Count; i < magnitude * characterNbByMagnitude; i++) { var spawnPoint = arPlane.center + new Vector3(Random.Range(-arPlane.extents.x, arPlane.extents.x), 0, Random.Range(-arPlane.extents.y, arPlane.extents.y)); hitPose.position = spawnPoint; hitPose.rotation = Quaternion.Euler(0, Random.Range(-180, 180), 0); anchor = anchorManager.AttachAnchor(arPlane, hitPose); anchors.Add(anchor); } } }
private void CreateAnchor() { Vector3 screenPoint = DepthSource.ARCamera.WorldToScreenPoint(transform.position); // Raycasts against the location the object stopped. TrackableType trackableTypes = TrackableType.Planes | TrackableType.FeaturePoint; List <ARRaycastHit> raycastHits = new List <ARRaycastHit>(); if (_raycastManager.Raycast(screenPoint, raycastHits, trackableTypes)) { ARRaycastHit raycastHit = raycastHits[0]; ARAnchor anchor = null; if ((raycastHit.trackable is ARPlane) && Vector3.Dot(DepthSource.ARCamera.transform.position - raycastHit.pose.position, raycastHit.pose.rotation * Vector3.up) < 0) { Debug.Log("Hit at back of the current ARPlane."); } else if (raycastHit.trackable is ARPlane plane) { Debug.Log("Create ARAnchor attached to ARPlane."); anchor = _anchorManager.AttachAnchor(plane, raycastHit.pose); } else { Debug.Log("Create a regular ARAnchor."); anchor = new GameObject().AddComponent <ARAnchor>(); anchor.gameObject.name = "ARAnchor"; anchor.transform.position = raycastHit.pose.position; anchor.transform.rotation = raycastHit.pose.rotation; } if (anchor != null) { transform.SetParent(anchor.transform, true); } } }
// Update is called once per frame void Update() { if (!RobbieManager.Instance.isRobbieActive && Input.touchCount > 0) { Touch touch = Input.GetTouch(0); if (touch.phase == TouchPhase.Began) { if (raycastManager.Raycast(touch.position, hits, TrackableType.PlaneWithinPolygon)) { ARRaycastHit hit = hits[0]; Pose hitPose = hit.pose; if (hit.trackable is ARPlane plane) { ARAnchor toAttach = anchorManager.AttachAnchor(plane, hitPose); if (toAttach != null) { RobbieManager.Instance.PositionRobbie(toAttach); } } } } } }
private void Update() { // Deselect the previously hovered planes foreach (TrackableId id in m_hoveredPlanes) { if (id == TrackableId.invalidId) { continue; } ARPlane plane = m_planeManager.GetPlane(id); if (plane == null) { continue; } // Don't hide planes which have anchors on them if (m_planesWithAnchors.Contains(id)) { continue; } plane.gameObject.SetActive(false); } // Raycasting and updating the scene for each hand for (int i = 0; i < 2; i++) { InputDevice device = InputDevices.GetDeviceAtXRNode((i == 0) ? XRNode.LeftHand : XRNode.RightHand); if (!device.TryGetFeatureValue(CommonUsages.isTracked, out bool isTracked) || !isTracked || !device.TryGetFeatureValue(CommonUsages.primaryButton, out bool isHandTapping) || !device.TryGetFeatureValue(PointerPosition, out Vector3 handPosition) || !device.TryGetFeatureValue(PointerRotation, out Quaternion handRotation)) { m_raycastRayIndicators[i].SetActive(false); m_hoveredPlanes[i] = TrackableId.invalidId; m_wasHandTapping[i] = true; // Prevent detecting a tap when we begin tracking the hand again continue; } bool handTappedThisUpdate = isHandTapping && !m_wasHandTapping[i]; m_wasHandTapping[i] = isHandTapping; m_raycastRayIndicators[i].transform.SetPositionAndRotation(handPosition, handRotation); m_raycastRayIndicators[i].SetActive(true); Vector3 handForward = new Pose(handPosition, handRotation).forward; List <ARRaycastHit> raycastHits = new List <ARRaycastHit>(); if (m_raycastManager.Raycast(new Ray(handPosition, handForward), raycastHits, TrackableType.PlaneWithinPolygon)) { // Raycast hits are sorted by distance, so the first one will be the closest hit. var raycastHit = raycastHits[0]; ARPlane plane = raycastHit.trackable as ARPlane; plane.gameObject.SetActive(true); m_hoveredPlanes[i] = plane.trackableId; if (handTappedThisUpdate) { m_anchorManager.AttachAnchor(plane, raycastHit.pose); m_planesWithAnchors.Add(plane.trackableId); } } else { m_hoveredPlanes[i] = TrackableId.invalidId; } } }