Files
RoomAwareVR/Assets/_Scripts/Utility/SpawnAtLongestEdge.cs
T
Thorbjoern 643c071c51 First try
2025-06-28 16:40:31 +02:00

78 lines
2.8 KiB
C#

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR;
public class SpawnAtLongestEdge : MonoBehaviour
{
[SerializeField] GameObject roomPrefab;
[SerializeField] GameObject pointPrefab;
[SerializeField] GameObject reticlePrefab;
// Start is called before the first frame update
void Start()
{
// Prüfe, ob die Boundary aktiviert ist
if (OVRManager.boundary != null)
{
// Hole die Boundary-Geometrie (Play Area)
Vector3[] boundaryPoints = OVRManager.boundary.GetGeometry(OVRBoundary.BoundaryType.PlayArea);
if (boundaryPoints != null && boundaryPoints.Length > 0)
{
Debug.Log("Boundary Points gefunden: " + boundaryPoints.Length);
AlignObjectToLongestEdge(boundaryPoints);
}
else
{
Debug.LogWarning("Keine Boundary Points gefunden.");
}
}
else
{
Debug.LogError("OVRManager.boundary ist nicht aktiviert.");
}
}
void AlignObjectToLongestEdge(Vector3[] boundaryPoints)
{
float maxLength = 0f;
Vector3 longestEdgeStart = Vector3.zero;
Vector3 longestEdgeEnd = Vector3.zero;
// Iteriere über die Punkte und finde die längste Kante
for (int i = 0; i < boundaryPoints.Length; i++)
{
Vector3 start = boundaryPoints[i];
Vector3 end = boundaryPoints[(i + 1) % boundaryPoints.Length]; // Modulo für den letzten Punkt
// Debug Visuals for Points
Instantiate(pointPrefab, start, Quaternion.LookRotation(end));
float length = Vector3.Distance(start, end);
if (length > maxLength)
{
maxLength = length;
longestEdgeStart = start;
longestEdgeEnd = end;
}
}
Debug.Log($"Längste Kante gefunden: Start({longestEdgeStart}), Ende({longestEdgeEnd}), Länge: {maxLength}");
// Richte dein Objekt aus
Vector3 midPoint = (longestEdgeStart + longestEdgeEnd) / 2;
Vector3 direction = (longestEdgeEnd - longestEdgeStart).normalized;
Instantiate(roomPrefab, midPoint, Quaternion.LookRotation(direction));
Transform reticle = Instantiate(reticlePrefab, longestEdgeStart, Quaternion.LookRotation(direction)).transform;
reticle.position = new Vector3 (reticle.position.x - 0.75f, reticle.position.y, reticle.position.z + 1);
// Beispiel: Objekt positionieren und ausrichten
//Transform objTransform = this.transform;
//objTransform.position = midPoint;
//objTransform.rotation = Quaternion.LookRotation(direction);
Debug.Log("Objekt wurde an der längsten Kante ausgerichtet.");
}
}