Realization of click event and detection function based on Pico in Unity development

Pico click event and detection function implementation in Unity development

1. Create a RayCtrl script and mount it (ray detection refers to the article on ray detection on the homepage, so I won’t explain too much here)

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class RayCtrl : MonoBehaviour
{

    private LineRenderer line;
    public GameObject NowGameObject;

    private GameObject StartPos;
    public static Vector3 hitPos;
    public Transform dot;

    private RaycastHit hit;

    public static string name;

    // Use this for initialization
    void Start()
    {
        line = transform.Find("ray_LengthAdaptive").GetComponent<LineRenderer>();
        line.gameObject.SetActive(true);
        dot = transform.Find("dot");
        dot.gameObject.SetActive(true);
        StartPos = GameObject.Find("StartPos");
        hitPos = Vector3.zero;
    }

    // Update is called once per frame
    void Update()
    {
        Ray ray = new Ray { origin = StartPos.transform.position, direction = StartPos.transform.forward };
        line.SetPosition(0, ray.origin);
        if (Physics.Raycast(ray, out hit, 1000))
        {
            line.SetPosition(1, hit.point);
            line.startColor = Color.green;
            dot.position = hit.point;
            hitPos = hit.point;
            NowGameObject = hit.transform.gameObject;
            name = NowGameObject.name;
        }
        else
        {
            line.SetPosition(1, ray.origin + ray.direction * 2);
            line.startColor = Color.red;
            dot.position = ray.origin + ray.direction * 2;
            hitPos = ray.origin + ray.direction * 2;
            name = null;
        }
    }
}

The details are as follows:

2. Create code GameObjectManager

For the convenience of detection, I wrote a detection of left mouse button click here.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using Pvr_UnitySDKAPI;

public class GameObjectManager : MonoBehaviour
{


    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        if (RayCtrl.name == this.gameObject.name)
        {
            if (Input.GetMouseButton(0) || Controller.UPvr_GetKey(1, Pvr_KeyCode.TRIGGER))
            {

              //这里写需要的功能

            }
        }
    }
}

Here for testing, I wrote a rotation event.

Add the GameObjectManager script to the object that needs to interact (the object must have a collision body)

 

The test effect is shown in the figure:

At this point, the implementation of Pico's click event and detection functions in Unity development has been completed, and you can write according to your own needs. Pay attention, don't get lost, in the next section, I will explain the code of the common interactive functions of the Pico controller for fans.

Guess you like

Origin blog.csdn.net/weixin_53754310/article/details/123652637