generated from VR-Sexe/Unity3DTemplate
149 lines
4.7 KiB
C#
149 lines
4.7 KiB
C#
using Oculus.Interaction.PoseDetection;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using Unity.VisualScripting;
|
|
using UnityEngine;
|
|
using UnityEngine.UI;
|
|
|
|
public class MovableFurniture : MonoBehaviour
|
|
{
|
|
private List<Transform> children;
|
|
private List<ObjectOutline> outlines;
|
|
private Rigidbody _rigidbody;
|
|
|
|
private Bounds combinedBounds;
|
|
private Vector3 centerOffset;
|
|
// Start is called before the first frame update
|
|
void Start()
|
|
{
|
|
_rigidbody = GetComponent<Rigidbody>();
|
|
children = new List<Transform>(GetComponentsInChildren<Transform>());
|
|
outlines = new List<ObjectOutline>();
|
|
foreach (Transform child in children)
|
|
{
|
|
var part = child.gameObject.AddComponent<MovableFurniturePart>();
|
|
part.parent = this;
|
|
var outline = child.gameObject.AddComponent<ObjectOutline>();
|
|
outlines.Add(outline);
|
|
outline.OutlineMode = ObjectOutline.Mode.OutlineAll;
|
|
outline.OutlineColor = Color.yellow;
|
|
outline.OutlineWidth = 5f;
|
|
outline.enabled = false;
|
|
}
|
|
CalculateBounds();
|
|
}
|
|
|
|
// Update is called once per frame
|
|
void Update()
|
|
{
|
|
|
|
}
|
|
|
|
public void OnPointerClickUp()
|
|
{
|
|
if (_rigidbody != null)
|
|
{
|
|
_rigidbody.useGravity = true;
|
|
}
|
|
|
|
}
|
|
public void OnPointerClickDown()
|
|
{
|
|
if(_rigidbody!= null)
|
|
{
|
|
_rigidbody.useGravity= false;
|
|
}
|
|
|
|
}
|
|
|
|
public void OnPointerEnter()
|
|
{
|
|
foreach (ObjectOutline outline in outlines)
|
|
{
|
|
/*child.OutlineMode = ObjectOutline.Mode.OutlineAll;
|
|
child.OutlineColor = Color.yellow;
|
|
child.OutlineWidth = 5f;*/
|
|
outline.enabled = true;
|
|
}
|
|
}
|
|
|
|
public void OnPointerExit()
|
|
{
|
|
foreach (ObjectOutline outline in outlines)
|
|
{
|
|
outline.enabled = false;
|
|
}
|
|
}
|
|
|
|
public void SnapTo(Vector3 position, LayerMask layerMask)
|
|
{
|
|
Vector3 displacement = Vector3.zero;
|
|
this.transform.position = position+centerOffset;
|
|
Vector3[] directions = { Vector3.right, Vector3.left, Vector3.up, Vector3.down, Vector3.forward, Vector3.back };
|
|
int[] extents = { 0,0,1,1,2,2 };
|
|
|
|
for (int i = 0; i<6; i++ )
|
|
{
|
|
Ray raycast = new Ray(transform.position - centerOffset - (directions[i] * combinedBounds.extents[extents[i]]), (directions[i] * combinedBounds.size[extents[i]]));
|
|
RaycastHit hit;
|
|
bool SnapbHit = Physics.Raycast(raycast, out hit, combinedBounds.size[extents[i]], layerMask);
|
|
if (SnapbHit)
|
|
{
|
|
displacement[i / 2] -= (i % 2 == 1 ? -1 : 1) * (combinedBounds.size[extents[i]] - (hit.distance));
|
|
}
|
|
}
|
|
|
|
/*Ray raycast = new Ray((transform.position - centerOffset) - (new Vector3(0, 0, combinedBounds.extents.z)), (new Vector3(0, 0, combinedBounds.size.z )));
|
|
RaycastHit hit;
|
|
bool SnapbHit = Physics.Raycast(raycast, out hit, combinedBounds.size.z, layerMask);
|
|
if (SnapbHit)
|
|
{
|
|
Debug.Log("Hit");
|
|
//displacement.z -= (0 % 2 == 1 ? -1 : 1) * (2*combinedBounds.size.z - (hit.distance));
|
|
displacement.z -= (combinedBounds.size.z - (hit.distance));
|
|
}*/
|
|
Debug.Log(displacement);
|
|
this.transform.Translate(displacement, Space.World);
|
|
}
|
|
|
|
|
|
void OnDrawGizmos()
|
|
{
|
|
// Draw a yellow sphere at the transform's position
|
|
Gizmos.color = Color.red;
|
|
Gizmos.DrawWireCube(transform.position - centerOffset, combinedBounds.size);
|
|
Gizmos.DrawRay((transform.position - centerOffset) - (new Vector3(0,0,combinedBounds.extents.z)), (new Vector3(0, 0, combinedBounds.size.z)));
|
|
}
|
|
|
|
private void CalculateBounds()
|
|
{
|
|
if (GetComponent<Renderer>() != null)
|
|
{
|
|
combinedBounds = GetComponent<Renderer>().bounds;
|
|
}
|
|
else
|
|
{
|
|
combinedBounds = new Bounds(transform.position, Vector3.zero);
|
|
}
|
|
var renderers = GetComponentsInChildren<Collider>();
|
|
foreach (Collider child in renderers)
|
|
{
|
|
if (child != GetComponent<Renderer>())
|
|
{
|
|
if (combinedBounds == null)
|
|
{
|
|
combinedBounds = child.bounds;
|
|
}
|
|
else
|
|
{
|
|
combinedBounds.Encapsulate(child.bounds);
|
|
}
|
|
}
|
|
}
|
|
//combinedBounds = renderers[0].bounds;
|
|
Debug.Log(combinedBounds.center);
|
|
centerOffset = transform.position - combinedBounds.center;
|
|
}
|
|
}
|