Closed petrepa closed 4 years ago
Followed this tutorial: AR Foundation Object Placement - Unity Augmented Reality/AR
Will try to add posibility to rotate by using two fingers. Y-axis.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.ARFoundation;
using UnityEngine.XR.ARSubsystems;
[RequireComponent(typeof(ARRaycastManager))]
public class SpawnObjectOnPlane : MonoBehaviour
{
private ARRaycastManager raycastManager;
private GameObject spawnedObject;
public GameObject PlaceablePrefab;
static List<ARRaycastHit> s_Hits = new List<ARRaycastHit>();
private void Awake() {
raycastManager = GetComponent<ARRaycastManager>();
}
bool TryGetTouchPosition(out Vector2 touchPosition){
if(Input.touchCount > 0){
touchPosition = Input.GetTouch(0).position;
return true;
}
touchPosition = default;
return false;
}
private void Update() {
if(!TryGetTouchPosition(out Vector2 touchPosition)){
return;
}
if(raycastManager.Raycast(touchPosition, s_Hits, TrackableType.PlaneWithinPolygon)){
var hitPose = s_Hits[0].pose;
if(spawnedObject == null){
spawnedObject = Instantiate(PlaceablePrefab, hitPose.position, hitPose.rotation);
}
else{
spawnedObject.transform.position = hitPose.position;
spawnedObject.transform.rotation = hitPose.rotation;
}
}
if(Input.touchCount == 2){
float rotateSpeed = 0.09f;
Touch touchZero = Input.GetTouch(0);
//Rotate the model based on offset
Vector3 localAngle = spawnedObject.transform.localEulerAngles;
localAngle.y -= rotateSpeed * touchZero.deltaPosition.x;
spawnedObject.transform.localEulerAngles = localAngle;
}
}
}
This is working, but is a bit finicky. The app will most likely register 1 finger first and move the chair before the second finger is registrered and the rotation can start. The chair also doesn't remember the rotation, so one has to rotate the chair each time the chair is moved to a new place.
Closing for now, but should be reopened at a later stage if possible.
Features 🥳🎉