generated from cepdnaclk/eYY-3yp-project-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3ddeee7
commit 95781d5
Showing
3 changed files
with
26 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,74 +1,47 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using UnityEngine; | ||
using UnityEngine.UI; | ||
using UnityEngine.XR.ARFoundation; | ||
using UnityEngine.XR.ARSubsystems; | ||
|
||
[RequireComponent(typeof(ARRaycastManager))] | ||
public class RandomObjectGeneration : MonoBehaviour | ||
{ | ||
[SerializeField] private ARPlaneManager arPlaneManager; | ||
|
||
|
||
|
||
public GameObject gameObjectToInstantiate; | ||
private ARRaycastManager raycastManager; | ||
|
||
|
||
private List<GameObject> spawnedObjects = new List<GameObject>(); | ||
private List<ARRaycastHit> hitList = new List<ARRaycastHit>(); | ||
|
||
private GameObject placedObject; | ||
|
||
private void Awake() | ||
{ | ||
raycastManager = GetComponent<ARRaycastManager>(); | ||
arPlaneManager= GetComponent<ARPlaneManager>(); | ||
arPlaneManager.planesChanged += PlaneChanged; | ||
} | ||
|
||
void Start() | ||
private void PlaneChanged(ARPlanesChangedEventArgs args) | ||
{ | ||
StartCoroutine(GenerateObjects()); | ||
} | ||
|
||
IEnumerator GenerateObjects() | ||
{ | ||
int maxObjects = 3; | ||
|
||
while (true) | ||
if (args.added != null && placedObject == null) | ||
{ | ||
if (spawnedObjects.Count < maxObjects) | ||
{ | ||
bool objectSpawned = false; | ||
|
||
if (raycastManager.Raycast(new Vector2(Screen.width * 0.5f, Screen.height * 0.5f), hitList, TrackableType.PlaneWithinPolygon)) | ||
{ | ||
var hitPose = hitList[0].pose; | ||
|
||
Vector3 randomOffset = new Vector3(Random.Range(-0.5f, 0.5f), 0, Random.Range(-0.5f, 0.5f)); | ||
Vector3 spawnPosition = hitPose.position + randomOffset; | ||
|
||
bool positionValid = true; | ||
foreach (GameObject obj in spawnedObjects) | ||
{ | ||
if (Vector3.Distance(obj.transform.position, spawnPosition) < 0.3f) | ||
{ | ||
positionValid = false; | ||
break; | ||
} | ||
} | ||
|
||
if (positionValid) | ||
{ | ||
GameObject newObject = Instantiate(gameObjectToInstantiate, spawnPosition, hitPose.rotation); | ||
spawnedObjects.Add(newObject); | ||
objectSpawned = true; | ||
} | ||
} | ||
|
||
if (!objectSpawned) | ||
{ | ||
// If no object was spawned in the current iteration, yield for a shorter duration before retrying | ||
yield return new WaitForSeconds(0.1f); | ||
} | ||
} | ||
else | ||
{ | ||
// If max objects reached, yield for a longer duration before checking again | ||
yield return new WaitForSeconds(0.5f); | ||
} | ||
ARPlane aRPlane = args.added[0]; | ||
placedObject = Instantiate(gameObjectToInstantiate, aRPlane.transform.position, Quaternion.identity); | ||
|
||
} | ||
} | ||
void Start() | ||
{ | ||
|
||
} | ||
|
||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters