ViveInputUtility-handle rays interact with 3D objects (5)

This chapter mainly introduces the two parts of using the handle ray ray to enter, click, leave the object, and ray to pick up the object through VIU.

1. Preliminary preparation
Here, we will explain based on the teleportation scene in Chapter 2. The link is as follows:
We named the stairs Stairs, and the wall Wall, so that we can test the handle rays entering, clicking, and preparing for leaving objects.

2. The entry, click, and departure of 3D object detection rays
Write the Demo4_RayDetectionTest script and mount it on the stairs and walls at the same time. The triggering condition: Collider has been mounted on the object

using System.Collections.Generic;
using UnityEngine;
using HTC.UnityPlugin.Vive;
using UnityEngine.EventSystems;

public class Demo4_RayDetectionTest : MonoBehaviour,
            IPointerEnterHandler,
            IPointerExitHandler,
            IPointerClickHandler
{
    
    
    private HashSet<PointerEventData> hovers = new HashSet<PointerEventData>();     //射线Hover哈希表

    /// <summary>
    /// 射线进入
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerEnter(PointerEventData eventData)
    {
    
    
        if (hovers.Add(eventData) && hovers.Count == 1)
        {
    
    
            Debug.Log($"射线进入 {
      
      transform.name}");
        }
    }

    /// <summary>
    /// 射线离开
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerExit(PointerEventData eventData)
    {
    
    
        if (hovers.Remove(eventData) && hovers.Count == 0)
        {
    
    
            Debug.Log($"射线离开 {
      
      transform.name}");
        }
    }

    /// <summary>
    /// 射线点击
    /// </summary>
    /// <param name="eventData"></param>
    public void OnPointerClick(PointerEventData eventData)
    {
    
    
        VivePointerEventData viveEventData = eventData as VivePointerEventData;
        if (viveEventData != null)
        {
    
    
            if (viveEventData.viveButton == ControllerButton.Trigger)
            {
    
    
                Debug.Log($"射线点击 {
      
      transform.name}");
            }
        }
    }
}

Run the test, the ladder and the wall will trigger the ray to enter, click, and leave
insert image description here
2. Drag and drop the 3D object.
It is very simple to realize the function of 3D object ray picking. Just mount the Draggable component on the object directly. The dragging prerequisite: on the object The Collider has been mounted, and the effect after running: the ray is aimed at the ladder/wall, the object can be picked up by pulling the trigger button, and the object can be picked up by holding down the hold button and moved to a certain distance from the player, which can be controlled by the touchpad The length of the pickup distance.

3. Detailed explanation of Draggable components
insert image description here
(1) Init Grab Distance: The distance between the object and the player when it is initially picked up
(2) Ovveride Max Angular Velociry: Covers the maximum angular velocity
(3) Unblockable Grab: Unblocked pickup
(4) Scrolling Spped: Stretching rate, The larger the value, the higher the object distance adjustment rate
(5) Min Stretch Scale: The minimum scaling value, which takes effect when two handle rays are picked up at the same time
(6) Max Stretch Scale: The maximum scaling value, which takes effect when two handle rays are picked up at the same time

In our actual development process, we are more concerned with realizing functions and interactions, such as: the player stands on the ground and needs to install the device to the ceiling.
In the above case, in addition to placing a ladder in the scene and dragging the device to teleport to a certain height for installation, we can also use the After Grabbed, Before Release, and On Drop events of the Draggable component to directly pick up and interact with rays on the ground It can be installed to the designated position by adjusting the distance.
Write the Demo4_RayDrag script, mount it on the ladder and the wall at the same time, trigger the prerequisite: Collider has been mounted on the object

using UnityEngine;

public class Demo4_RayDrag : MonoBehaviour
{
    
    
    private Draggable draggable;

    private void Awake()
    {
    
    
        draggable = GetComponent<Draggable>();
    }

    private void Start()
    {
    
    
        draggable.afterGrabberGrabbed += Draggable_afterGrabberGrabbed;
        draggable.beforeGrabberReleased += Draggable_beforeGrabberReleased;
        draggable.onGrabberDrop += Draggable_onGrabberDrop;
    }

    private void Draggable_afterGrabberGrabbed()
    {
    
    
        Debug.Log($"{
      
      transform.name} 已被拿起");
    }

    private void Draggable_beforeGrabberReleased()
    {
    
    
        Debug.Log($"{
      
      transform.name} 放下之前");
    }

    private void Draggable_onGrabberDrop()
    {
    
    
        Debug.Log($"放下 {
      
      transform.name}");
    }
}

It is not difficult to see that through the three states in the script, the solution to the above cases is as follows:
(1) After being picked up, the installation position is highlighted
(2) Collision detection is performed before it is put down. If it is the installation position, the logic is performed Solution
(3) After putting it down, the installation position is not highlighted

Guess you like

Origin blog.csdn.net/weixin_38484443/article/details/127320304