I want the script below ideally function like:
- tap then place ONE duck
- after that, I tap again show UI
Now it is working on the second point (show UI) but I will still spawn a new duck even there is already one duck.
How should I modify so that user can only tap and place ONE duck?
public GameObject duck;
public GameObject canvas;
public GameObject confirmationUI;
public GameObject inputUI;
private ARRaycastManager aRRaycastManager;
private ARPlaneManager aRPlaneManager;
private bool isDuckSpawned = false;
private GameObject spawnedObject;
private List<ARRaycastHit> hits = new List<ARRaycastHit>();
public LayerMask ignoreARPlaneMask;
private void Awake()
{
aRRaycastManager = GetComponent<ARRaycastManager>();
aRPlaneManager = GetComponent<ARPlaneManager>();
confirmationUI.SetActive(false);
inputUI.SetActive(false);
}
void Update()
{
if (Input.GetMouseButtonDown(0))
{
Debug.Log("Mouse clicked");
if (!isDuckSpawned && aRRaycastManager.Raycast(Input.mousePosition, hits, TrackableType.PlaneWithinPolygon))
{
Debug.Log("Duck Gonna be Spawned");
if (hits.Count > 0)
{
ARRaycastHit ARhit = hits[0];
ARPlane plane = aRPlaneManager.GetPlane(ARhit.trackableId);
if (plane.alignment == PlaneAlignment.HorizontalUp)
{
Debug.Log("Error D");
Pose pose = ARhit.pose;
if (spawnedObject == null)
{
Vector3 spawnDuckPosition = new Vector3(pose.position.x, pose.position.y + 0.03f, pose.position.z);
spawnedObject = Instantiate(duck, spawnDuckPosition, pose.rotation);
Debug.Log("Error C");
spawnedObject.tag = "Placed Duck";
isDuckSpawned = true;
Debug.Log("Duck Spawned!");
}
else
{
Debug.Log("Duck already spawned, ignoring new spawn request.");
}
}
}
}
else
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
RaycastHit hit;
Debug.Log("Error A");
if (Physics.Raycast(ray, out hit, 100, ignoreARPlaneMask))
{
Debug.Log("Hit: " + hit.transform.name + " (" + hit.transform.tag + ")");
Debug.Log("Error B");
if (hit.collider.CompareTag("Placed Duck"))
{
Debug.Log("Duck clicked - Showing UI");
showUI();
return;
}
}
}
}
}