TheUnityWorkbench / tuw-arfoundation-demo

Other
103 stars 89 forks source link

Please update for Unity 2019.2.16f1 #4

Open dremond71 opened 4 years ago

dremond71 commented 4 years ago

Hi,

I came across your cool demo application

https://www.youtube.com/watch?v=Ml2UakwRxjk&t=1218s

I am a newbie with Unity programming, but a veteran with android programming.

The recent version of unity changed, and I guess some of the code you are using is out-dated.

If you could please update your application, that would be very helpful....and appreciated.

Thanks

:)

YoboZorle commented 2 years ago

Code updated

`using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.XR.ARFoundation; using UnityEngine.Experimental.XR; using System; using UnityEngine.XR.ARSubsystems;

public class ARTapToPlaceObject : MonoBehaviour { public GameObject objectToPlace; public GameObject placementIndicator;

private ARRaycastManager arOrigin;
private Pose placementPose;
private bool placementPoseIsValid = false;

void Start()
{
    arOrigin = FindObjectOfType<ARRaycastManager>();
}

void Update()
{
    UpdatePlacementPose();
    UpdatePlacementIndicator();

    if (placementPoseIsValid && Input.touchCount > 0 && Input.GetTouch(0).phase == TouchPhase.Began)
    {
        PlaceObject();
    }
}

private void PlaceObject()
{
    Instantiate(objectToPlace, placementPose.position, placementPose.rotation);
}

private void UpdatePlacementIndicator()
{
    if (placementPoseIsValid)
    {
        placementIndicator.SetActive(true);
        placementIndicator.transform.SetPositionAndRotation(placementPose.position, placementPose.rotation);
    }
    else
    {
        placementIndicator.SetActive(false);
    }
}

private void UpdatePlacementPose()
{
    var screenCenter = Camera.current.ViewportToScreenPoint(new Vector3(0.5f, 0.5f));
    var hits = new List<ARRaycastHit>();
    arOrigin.Raycast(screenCenter, hits, TrackableType.Planes);

    placementPoseIsValid = hits.Count > 0;
    if (placementPoseIsValid)
    {
        placementPose = hits[0].pose;

        var cameraForward = Camera.current.transform.forward;
        var cameraBearing = new Vector3(cameraForward.x, 0, cameraForward.z).normalized;
        placementPose.rotation = Quaternion.LookRotation(cameraBearing);
    }
}

}`