Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //======= Copyright (c) Valve Corporation, All rights reserved. ===============
- //
- // Purpose: Demonstrates how to create a simple interactable object
- //
- //=============================================================================
- using UnityEngine;
- using System.Collections;
- namespace Valve.VR.InteractionSystem
- {
- //-------------------------------------------------------------------------
- [RequireComponent(typeof(Interactable))]
- public class BoardInteraction : MonoBehaviour
- {
- private Vector3 oldPosition;
- private Quaternion oldRotation;
- private float attachTime;
- private Hand.AttachmentFlags attachmentFlags = Hand.defaultAttachmentFlags & (~Hand.AttachmentFlags.SnapOnAttach) & (~Hand.AttachmentFlags.DetachOthers);
- private bool lineActive;
- //Private
- private int numCLicks = 0;
- private GameObject line;
- private LineRenderer currentLine;
- public float width = 0.05f;
- public Color color = Color.black;
- private MarkerTip marker;
- //-------------------------------------------------
- void Awake()
- {
- }
- //-------------------------------------------------
- // Called when a Hand starts hovering over this object
- //-------------------------------------------------
- private void OnHandHoverBegin(Hand hand)
- {
- }
- //-------------------------------------------------
- // Called when a Hand stops hovering over this object
- //-------------------------------------------------
- private void OnHandHoverEnd(Hand hand)
- {
- }
- //-------------------------------------------------
- // Called every Update() while a Hand is hovering over this object
- //-------------------------------------------------
- private void HandHoverUpdate(Hand hand)
- {
- if (hand.GetStandardInteractionButtonDown() || ((hand.controller != null) && hand.controller.GetPressDown(Valve.VR.EVRButtonId.k_EButton_Grip)))
- {
- if (hand.currentAttachedObject != gameObject)
- {
- // Save our position/rotation so that we can restore it when we detach
- oldPosition = transform.position;
- oldRotation = transform.rotation;
- // Call this to continue receiving HandHoverUpdate messages,
- // and prevent the hand from hovering over anything else
- hand.HoverLock(GetComponent<Interactable>());
- // Attach this object to the hand
- hand.AttachObject(gameObject, attachmentFlags);
- }
- else
- {
- // Detach this object from the hand
- hand.DetachObject(gameObject);
- // Call this to undo HoverLock
- hand.HoverUnlock(GetComponent<Interactable>());
- // Restore position/rotation
- //transform.position = oldPosition;
- //transform.rotation = oldRotation;
- }
- }
- }
- private void OnMarkerHoverBegin(MarkerTip marker)
- {
- line = new GameObject();
- currentLine = line.AddComponent<LineRenderer>();
- currentLine.startWidth = width;
- currentLine.endWidth = width;
- currentLine.startColor = Color.grey;
- currentLine.endColor = Color.grey;
- Debug.Log(currentLine.material);
- numCLicks = 0;
- lineActive = true;
- this.marker = marker;
- }
- void Update()
- {
- if (lineActive)
- {
- currentLine.positionCount = numCLicks + 1;
- currentLine.SetPosition(numCLicks, new Vector3(marker.transform.position.x, marker.transform.position.y, transform.position.z - 0.01f));
- numCLicks++;
- }
- }
- //this update is i guesss what you wanted ^^.... from the below.
- private void OnMarkerHoverUpdate(MarkerTip marker)
- {
- }
- private void OnMarkerHoverEnd(MarkerTip marker)
- {
- lineActive = false;
- }
- //-------------------------------------------------
- // Called when this GameObject becomes attached to the hand
- //-------------------------------------------------
- private void OnAttachedToHand(Hand hand)
- {
- attachTime = Time.time;
- }
- //-------------------------------------------------
- // Called when this GameObject is detached from the hand
- //-------------------------------------------------
- private void OnDetachedFromHand(Hand hand)
- {
- }
- //-------------------------------------------------
- // Called every Update() while this GameObject is attached to the hand
- //-------------------------------------------------
- private void HandAttachedUpdate(Hand hand)
- {
- }
- //-------------------------------------------------
- // Called when this attached GameObject becomes the primary attached object
- //-------------------------------------------------
- private void OnHandFocusAcquired(Hand hand)
- {
- }
- //-------------------------------------------------
- // Called when another attached GameObject becomes the primary attached object
- //-------------------------------------------------
- private void OnHandFocusLost(Hand hand)
- {
- }
- }
- }
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using Valve.VR.InteractionSystem;
- public class MarkerTip : MonoBehaviour {
- public Transform hoverSphereTransform;
- public float hoverSphereRadius = 0.05f;
- public LayerMask hoverLayerMask = -1;
- public float hoverUpdateInterval = 0.1f;
- public bool hoverLocked { get; private set; }
- private Interactable _hoveringInteractable;
- private int prevOverlappingColliders = 0;
- private const int ColliderArraySize = 16;
- private Collider[] overlappingColliders;
- private Player playerInstance;
- private void Awake()
- {
- if (hoverSphereTransform == null)
- {
- hoverSphereTransform = this.transform;
- }
- }
- // Use this for initialization
- void Start () {
- playerInstance = Player.instance;
- // allocate array for colliders
- overlappingColliders = new Collider[ColliderArraySize];
- hoverLocked = false;
- }
- // Update is called once per frame
- void Update ()
- {
- UpdateHovering();
- }
- //-------------------------------------------------
- // The Interactable object this Hand is currently hovering over
- //-------------------------------------------------
- public Interactable hoveringInteractable
- {
- get { return _hoveringInteractable; }
- set
- {
- if (_hoveringInteractable != value)
- {
- if (_hoveringInteractable != null)
- {
- //_hoveringInteractable.SendMessage("OnHandHoverEnd", this, SendMessageOptions.DontRequireReceiver);
- _hoveringInteractable.SendMessage("OnMarkerHoverEnd", this, SendMessageOptions.DontRequireReceiver);
- }
- _hoveringInteractable = value;
- if (_hoveringInteractable != null)
- {
- //_hoveringInteractable.SendMessage("OnHandHoverBegin", this, SendMessageOptions.DontRequireReceiver);
- _hoveringInteractable.SendMessage("OnMarkerHoverBegin", this, SendMessageOptions.DontRequireReceiver);
- }
- }
- }
- }
- private void UpdateHovering()
- {
- if (hoverLocked)
- return;
- float closestDistance = float.MaxValue;
- Interactable closestInteractable = null;
- // Pick the closest hovering
- float flHoverRadiusScale = playerInstance.transform.lossyScale.x * 2.5f;
- float flScaledSphereRadius = hoverSphereRadius * flHoverRadiusScale;
- // if we're close to the floor, increase the radius to make things easier to pick up
- float handDiff = Mathf.Abs(transform.position.y - playerInstance.trackingOriginTransform.position.y);
- float boxMult = Util.RemapNumberClamped(handDiff, 0.0f, 0.5f * flHoverRadiusScale, 5.0f, 1.0f) * flHoverRadiusScale;
- // null out old vals
- for (int i = 0; i < overlappingColliders.Length; ++i)
- {
- overlappingColliders[i] = null;
- }
- //Physics.OverlapBoxNonAlloc(
- // hoverSphereTransform.position - new Vector3(0, flScaledSphereRadius * boxMult - flScaledSphereRadius, 0),
- // new Vector3(flScaledSphereRadius, flScaledSphereRadius * boxMult * 2.0f, flScaledSphereRadius),
- // overlappingColliders,
- // Quaternion.identity,
- // hoverLayerMask.value
- //);
- Physics.OverlapSphereNonAlloc(
- hoverSphereTransform.position - new Vector3(0, flScaledSphereRadius * boxMult - flScaledSphereRadius, 0),
- flScaledSphereRadius,
- overlappingColliders,
- hoverLayerMask.value
- );
- // DebugVar
- int iActualColliderCount = 0;
- foreach (Collider collider in overlappingColliders)
- {
- Debug.Log(collider);
- if (collider == null)
- continue;
- Interactable contacting = collider.GetComponentInParent<Interactable>();
- // Yeah, it's null, skip
- if (contacting == null)
- continue;
- // Ignore this collider for hovering
- IgnoreHovering ignore = collider.GetComponent<IgnoreHovering>();
- if (ignore != null)
- {
- if (ignore.onlyIgnoreHand == null || ignore.onlyIgnoreHand == this)
- {
- continue;
- }
- }
- // Best candidate so far...
- float distance = Vector3.Distance(contacting.transform.position, hoverSphereTransform.position);
- if (distance < closestDistance && !collider.Equals(this.GetComponentInParent<CapsuleCollider>()))
- {
- closestDistance = distance ;
- closestInteractable = contacting;
- }
- iActualColliderCount++;
- }
- // Hover on this one
- hoveringInteractable = closestInteractable;
- if (iActualColliderCount > 0 && iActualColliderCount != prevOverlappingColliders)
- {
- prevOverlappingColliders = iActualColliderCount;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement