Skip to content

Commit

Permalink
Merge pull request #28 from harith-abeysinghe/main
Browse files Browse the repository at this point in the history
Fixed Random Object Generation
  • Loading branch information
harith-abeysinghe authored Dec 10, 2023
2 parents 3ddeee7 + 95781d5 commit e10390d
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 52 deletions.
3 changes: 2 additions & 1 deletion AR Demo/Assets/Scenes/GameScreen.unity
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ RenderSettings:
m_ReflectionIntensity: 1
m_CustomReflection: {fileID: 0}
m_Sun: {fileID: 0}
m_IndirectSpecularColor: {r: 0, g: 0, b: 0, a: 1}
m_IndirectSpecularColor: {r: 0.44657898, g: 0.4964133, b: 0.5748178, a: 1}
m_UseRadianceAmbientProbe: 0
--- !u!157 &3
LightmapSettings:
Expand Down Expand Up @@ -1615,6 +1615,7 @@ MonoBehaviour:
m_Script: {fileID: 11500000, guid: e48110cbb4151e04e864b9042406bf52, type: 3}
m_Name:
m_EditorClassIdentifier:
arPlaneManager: {fileID: 1659859600}
gameObjectToInstantiate: {fileID: 2912470072421854214, guid: 38e04c9aa6f1ad040a42c61783ed74d2,
type: 3}
--- !u!1 &1703699202
Expand Down
71 changes: 22 additions & 49 deletions AR Demo/Assets/Scripts/RandomObjectGeneration.cs
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()
{

}



}
4 changes: 2 additions & 2 deletions AR Demo/ProjectSettings/ProjectSettings.asset
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ PlayerSettings:
loadStoreDebugModeEnabled: 0
bundleVersion: 1.0
preloadedAssets:
- {fileID: 4800000, guid: c9f956787b1d945e7b36e0516201fc76, type: 3}
- {fileID: 4800000, guid: 0945859e5a1034c2cb6dce53cb4fb899, type: 3}
- {fileID: 11400000, guid: 1eb95d77acb39444e8cf44313496a874, type: 2}
- {fileID: -6654145819923878069, guid: 58f40b12bbc864f3c96c6505a9a1e1e3, type: 2}
- {fileID: 11400000, guid: fdea7d63f5e36d34880021adf1f629d5, type: 2}
- {fileID: 4310551847955826307, guid: 6bb381a8ba31f114d9724357633b4625, type: 2}
- {fileID: 4800000, guid: c9f956787b1d945e7b36e0516201fc76, type: 3}
- {fileID: 4800000, guid: 0945859e5a1034c2cb6dce53cb4fb899, type: 3}
metroInputSource: 0
wsaTransparentSwapchain: 0
m_HolographicPauseOnTrackingLoss: 1
Expand Down

0 comments on commit e10390d

Please sign in to comment.