AR input doesn't work

I’m following a video, using AR Foundation to make a plane to put an object. (Code as below). But when I touch the screen it doesn’t work. On my testing Phone, the plane is already detected. But just can’t implement my touching and put the object in. I’m very confused. Need help please~

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

[RequireComponent(typeof(ARRaycastManager))]
// check the system for this component, if not, it will be added automatically

public class PlacementController : MonoBehaviour
{  
    [SerializeField]
    private GameObject placePrefab;

    public GameObject PlacePrefab
    {
        get { return placePrefab;}
        set{placePrefab = value;}
    }

    private ARRaycastManager arRaycastManager;
    private GameObject placedObject;

    static List<ARRaycastHit> hits = new List<ARRaycastHit>();

    private void Awake()
    {
        arRaycastManager = GetComponent<ARRaycastManager>();
    }
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        // chech touch of screen, if so, output the touch position
        if (TryGetTouchPosition(out Vector2 touchPosition))
            return;
       
        // check touch position - raycast hits object, Plane, to see if the touch raycast hit the plane
        // if yes, the hits points will be add the the Hit list
        if (arRaycastManager.Raycast(touchPosition, hits, TrackableType.PlaneWithinPolygon))
        {
            // raycast hits are sorted by distance, so the first one will be the closest one
            var hitPose = hits[0].pose;     // pose is = transform

            if (placedObject == null)
            {
                placedObject = Instantiate(placePrefab, hitPose.position, hitPose.rotation);
                    // put the object to the plane
            }
            else
            {
                placedObject.transform.position = hitPose.position;
            }
        }
    }

    // get touch on screen, if so, return the touch position (vector 2)
    bool TryGetTouchPosition(out Vector2 touchPosition)
    {
        if (Input.touchCount>0)
        {
            touchPosition = Input.GetTouch(0).position;
            return true;
        }

        touchPosition = default;
        return false;

    }
}

I think the culprit the the first if statement in the update function. ‘TryGetTouchPosition’ returns true when there is a touch on the screen. That if statement is basically saying “If there is a touch on the screen this frame, end the function” and skips everything else. change your if statement to if (!TryGetTouchPosition(out Vector2 touchPosition)) return;
The ! this will invert the value return by the ‘TryGetTouchPosition’ function and will now operate as “If there is NO touches on the screen, end the function”. Then when you touch the screen, the Update method won’t return early and you should get the model placed as desired. Hope this helps.

1 Like