generated from VR-Sexe/Unity3DTemplate
176 lines
5.6 KiB
C#
176 lines
5.6 KiB
C#
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.EventSystems;
|
|
using Valve.VR;
|
|
using Valve.VR.Extras;
|
|
|
|
public class FurnitureMover : MonoBehaviour
|
|
{
|
|
private SteamVR_LaserPointer steamVrLaserPointer;
|
|
Transform previousContact = null;
|
|
|
|
public SteamVR_Behaviour_Pose pose;
|
|
public SteamVR_Action_Boolean interactWithUI = SteamVR_Input.GetBooleanAction("InteractUI");
|
|
|
|
public LayerMask AutoPlacementEnvironmentMask;
|
|
|
|
public GameObject pointer;
|
|
private MovableFurniture Grabbing;
|
|
|
|
public Color color;
|
|
public float thickness = 0.002f;
|
|
public Color clickColor = Color.green;
|
|
private void Start()
|
|
{
|
|
if (pose == null)
|
|
pose = this.GetComponent<SteamVR_Behaviour_Pose>();
|
|
if (pose == null)
|
|
Debug.LogError("No SteamVR_Behaviour_Pose component found on this object", this);
|
|
|
|
if (interactWithUI == null)
|
|
Debug.LogError("No ui interaction action has been set on this component.", this);
|
|
|
|
|
|
pointer = GameObject.CreatePrimitive(PrimitiveType.Cube);
|
|
pointer.transform.parent = this.transform;
|
|
pointer.transform.localScale = new Vector3(thickness, thickness, 100f);
|
|
pointer.transform.localPosition = new Vector3(0f, 0f, 50f);
|
|
pointer.transform.localRotation = Quaternion.identity;
|
|
BoxCollider collider = pointer.GetComponent<BoxCollider>();
|
|
if (collider)
|
|
{
|
|
Object.Destroy(collider);
|
|
}
|
|
Material newMaterial = new Material(Shader.Find("Unlit/Color"));
|
|
newMaterial.SetColor("_Color", color);
|
|
pointer.GetComponent<MeshRenderer>().material = newMaterial;
|
|
}
|
|
private void Awake()
|
|
{
|
|
|
|
}
|
|
|
|
private void Update()
|
|
{
|
|
float dist = 100f;
|
|
Ray raycast = new Ray(transform.position, transform.forward);
|
|
RaycastHit hit;
|
|
bool bHit = Physics.Raycast(raycast, out hit);
|
|
|
|
|
|
Ray SnapRaycast = new Ray(transform.position, transform.forward);
|
|
RaycastHit SnapHit;
|
|
bool SnapbHit = Physics.Raycast(SnapRaycast, out SnapHit, 100f, AutoPlacementEnvironmentMask);
|
|
|
|
if (SnapbHit && Grabbing != null)
|
|
{
|
|
Grabbing.SnapTo(SnapHit.point, AutoPlacementEnvironmentMask);
|
|
}
|
|
|
|
|
|
|
|
if (previousContact && previousContact != hit.transform)
|
|
{
|
|
PointerEventArgs args = new PointerEventArgs();
|
|
args.fromInputSource = pose.inputSource;
|
|
args.distance = 0f;
|
|
args.flags = 0;
|
|
args.target = previousContact;
|
|
OnPointerOut(this, args);
|
|
previousContact = null;
|
|
}
|
|
if (bHit && previousContact != hit.transform)
|
|
{
|
|
PointerEventArgs argsIn = new PointerEventArgs();
|
|
argsIn.fromInputSource = pose.inputSource;
|
|
argsIn.distance = hit.distance;
|
|
argsIn.flags = 0;
|
|
argsIn.target = hit.transform;
|
|
OnPointerIn(this, argsIn);
|
|
previousContact = hit.transform;
|
|
}
|
|
if (!bHit)
|
|
{
|
|
previousContact = null;
|
|
}
|
|
if (bHit && hit.distance < 100f)
|
|
{
|
|
dist = hit.distance;
|
|
}
|
|
if (bHit && interactWithUI.GetStateDown(pose.inputSource))
|
|
{
|
|
PointerEventArgs argsClick = new PointerEventArgs();
|
|
argsClick.fromInputSource = pose.inputSource;
|
|
argsClick.distance = hit.distance;
|
|
argsClick.flags = 0;
|
|
argsClick.target = hit.transform;
|
|
OnPointerClickDown(this, argsClick);
|
|
}
|
|
if (bHit && interactWithUI.GetStateUp(pose.inputSource))
|
|
{
|
|
PointerEventArgs argsClick = new PointerEventArgs();
|
|
argsClick.fromInputSource = pose.inputSource;
|
|
argsClick.distance = hit.distance;
|
|
argsClick.flags = 0;
|
|
argsClick.target = hit.transform;
|
|
OnPointerClickUp(this, argsClick);
|
|
}
|
|
|
|
if (interactWithUI != null && interactWithUI.GetState(pose.inputSource))
|
|
{
|
|
pointer.transform.localScale = new Vector3(thickness * 5f, thickness * 5f, dist);
|
|
pointer.GetComponent<MeshRenderer>().material.color = clickColor;
|
|
}
|
|
else
|
|
{
|
|
pointer.transform.localScale = new Vector3(thickness, thickness, dist);
|
|
pointer.GetComponent<MeshRenderer>().material.color = color;
|
|
}
|
|
pointer.transform.localPosition = new Vector3(0f, 0f, dist / 2f);
|
|
}
|
|
private void OnPointerClickDown(object sender, PointerEventArgs e)
|
|
{
|
|
MovableFurniturePart clickHandler = e.target.GetComponent<MovableFurniturePart>();
|
|
if (clickHandler == null)
|
|
{
|
|
return;
|
|
}
|
|
clickHandler.OnPointerClickDown();
|
|
Grabbing = clickHandler.parent;
|
|
/*Grabbing.transform.SetParent(this.transform);*/
|
|
}
|
|
|
|
private void OnPointerClickUp(object sender, PointerEventArgs e)
|
|
{
|
|
if(Grabbing != null)
|
|
{
|
|
Grabbing.OnPointerClickUp();
|
|
/*Grabbing.transform.SetParent(this.transform.root);*/
|
|
Grabbing = null;
|
|
}
|
|
|
|
}
|
|
|
|
private void OnPointerOut(object sender, PointerEventArgs e)
|
|
{
|
|
MovableFurniturePart pointerExitHandler = e.target.GetComponent<MovableFurniturePart>();
|
|
if (pointerExitHandler == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
pointerExitHandler.OnPointerExit();
|
|
}
|
|
|
|
private void OnPointerIn(object sender, PointerEventArgs e)
|
|
{
|
|
MovableFurniturePart pointerEnterHandler = e.target.GetComponent<MovableFurniturePart>();
|
|
if (pointerEnterHandler == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
|
|
pointerEnterHandler.OnPointerEnter();
|
|
}
|
|
} |