Phillipes_Fablab/Assets/Catalog.cs
2023-01-13 18:34:02 +01:00

72 lines
2.2 KiB
C#

using System.Collections;
using UnityEngine;
using Valve.VR.InteractionSystem;
public class Catalog : MonoBehaviour
{
private Player player = null;
public int maxShownObjects = 5;
public int angle = 180;
public float distance = .5f;
public float objectScale = .1f;
// Start is called before the first frame update
void Start()
{
player = Player.instance;
if (player == null)
{
Debug.LogError("<b>[SteamVR Interaction]</b> Teleport: No Player instance found in map.", this);
Destroy(this.gameObject);
return;
}
ShowCatalog();
}
// Update is called once per frame
void Update()
{
}
private void ShowCatalog()
{
gameObject.transform.SetParent(player.hands[0].transform);
gameObject.transform.localPosition = Vector3.zero;
float radians = angle * Mathf.Deg2Rad;
float angleIncrement = radians / (maxShownObjects - 1);
for (float i = 0 / 2; i < maxShownObjects; i++)
{
var j = i * angleIncrement - radians / 2;
var sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.parent = transform;
sphere.transform.localScale = Vector3.one * objectScale;
sphere.transform.localPosition = new Vector3(Mathf.Sin(j), 0, Mathf.Cos(j)) * distance;
}
StartCoroutine(CatalogCoroutine());
}
private IEnumerator CatalogCoroutine()
{
var hand = player.hands;
while (true)
{
transform.rotation = Quaternion.LookRotation(transform.position - player.hmdTransform.position);
transform.rotation = new Quaternion(0, transform.rotation.y, 0, transform.rotation.w);
yield return null;
}
}
private void OnDrawGizmosSelected()
{
float radians = angle * Mathf.Deg2Rad;
float angleIncrement = radians / (maxShownObjects - 1);
for (float i = 0 / 2; i < maxShownObjects; i++)
{
var j = i * angleIncrement - radians / 2;
Gizmos.DrawWireSphere(transform.position + new Vector3(Mathf.Sin(j), 0, Mathf.Cos(j)) * distance, objectScale / 2);
}
}
}