I am new to Unity and have no coding experience.
I am following this AR manual: Placing and Manipulating Objects in AR - Unity Learn
I am stuck on step 13:
Drag the SpawnableObject Prefab from the Project window into the Spawnable Prefab variable (figure 12)
I don’t have the ‘Raycast Manager’ and ‘Spawnable Prefab’ variables in my Inspector window under the Spawnable Manager (script).
This is the code in the file:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using unityengine.xr.arfoundation;
public class SpawnableManager : MonoBehaviour
{
[SerializeField]
ARRaycastManager m_RaycastManager;
List<ARRaycastHit> m_Hits = new List<ARRaycastHit>();
[SerializeField]
GameObject spawnablePrefab;
Camera arCam;
GameObject spawnedObject;
// Start is called before the first frame update
void Start()
{
spawnedObject = null;
arCam = GameObject.Find("AR Camera").GetComponent<Camera>();
}
// Update is called once per frame
void Update()
{
if (Input.touchCount == 0)
return;
RaycastHit hit;
Ray ray = arCam.ScreenPointToRay(Input.GetTouch(0).position);
if (m_RaycastManager.Raycast(Input.GetTouch(0).position, m_Hits))
{
if(Input.GetTouch(0).phase == TouchPhase.Began && spawnedObject == null)
{
if (Physics.(ray, out hit))
{
if (hit.collider.gameObject.tag == "Spawnable")
{
spawnedObject = hit.collider.gameObject;
}
else
{
SpawnPrefab(m_Hits[0].pose.position);
}
}
}
else if(Input.GetTouch(0).phase == TouchPhase.Moved && spawnedObject != null)
{
spawnedObject.transform.position = m_Hits[0].pose.position;
}
if(Input.GetTouch(0).phase == TouchPhase.Ended)
{
spawnedObject = null;
}
}
}
private void SpawnPrefab(Vector3 spawnPosition)
{
spawnedObject = Instantiate(spawnablePrefab, spawnPosition, Quaternion.identity);
}
}