Home>


Use unity and oculus go to display and move objects linked to the controller's movements
The object is an elongated Cube, and I want to make it disappear when I touch other objects

What you do
here or The controller was displayed on the screen as described here< br /> Imported Oculus integration, replaced the main camera with a VR camera, and attached "TrackedRemote" to "LeftHandAnchor" and "RightHandAnchor" in "OVRCameraRig"
In addition, we have succeeded in displaying the laser pointer using Line Renderer with the method introduced

・ I tried the subject matter
At first I tried to add a hit judgment to the laser pointer
I tried attaching Line Renderer to LaserPointer and adding the following code to the Cube side script, but nothing happened
It seems that the actual collision with Cube is Line Renderer, not LaserPointer

void OnCollisionEnter (Collision other)
    {
        if (other.gameObject.name == "LaserPointer")
        {
            Destroy (this.gameObject);
        }
        if (other.gameObject.name == "_LaserPointerRenderer")
        {
            Destroy (this.gameObject);
        }
        if (other.gameObject.name == "Laser Pointer Renderer")
        {
            Destroy (this.gameObject);
        }
}

Next, I tried to create a prefab with a 3D object Cube that was elongated and created it at the start, and then tried to update the coordinates every second to match the controller position. Was not displayed)
Since LaserPointer is also doing the acquisition of the controller etc., this processing was described in LaserPointer
The LaserPointer code is as follows
The laser itself is not displayed because it gets in the way
Also, Sword (the object I want to display this time) will be displayed if you specify the coordinates for Instantiate normally, but it will be left on the screen

LaserPointer.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/ **
 * Class that emits a laser pointer
 * /
public class LaserPointer: MonoBehaviour
{
    [SerializeField]
    private Transform _RightHandAnchor;// right hand
    [SerializeField]
    private Transform _LeftHandAnchor;// left hand
    [SerializeField]
    private Transform _CenterEyeAnchor;// center of eyes
    [SerializeField]
    private float _MaxDistance = 100.0f;// distance
    [SerializeField]
    private LineRenderer _LaserPointerRenderer;// LineRenderer
    [SerializeField]
    private GameObject Sword;// LineRenderer
    // bullet prefab
    [SerializeField]
    private GameObject bulletPrefab;
    // Power to blow bullets
    [SerializeField]
    private float bulletPower = 500f;

    // Shoot the enemy
    private void Shot (Transform pointer)
    {
        // create a bullet instance
        var bulletInstance = GameObject.Instantiate (bulletPrefab, pointer.position, pointer.rotation) as GameObject;
        // bullet firingbulletInstance.GetComponent&lt;Rigidbody&gt;(). AddForce (bulletInstance.transform.forward * bulletPower);
        // 5 to disappear automatically after 5 seconds
        Destroy (bulletInstance, 8f);
    }
    // controller
    private Transform Pointer
    {
        get
        {
            // Get the currently active controller
            var controller = OVRInput.GetActiveController ();
            if (controller == OVRInput.Controller.RTrackedRemote)
            {
                return _RightHandAnchor;
            }
            else if (controller == OVRInput.Controller.LTrackedRemote)
            {
                return _LeftHandAnchor;
            }
            // If i can't get either, the beam will come out
            return _CenterEyeAnchor;
        }
    }
    void Update ()
    {
        var pointer = Pointer;// get controller
                               // No controller or nothing without LineRenderer
        if (pointer == null || _LaserPointerRenderer == null)
        {
            return;
        }
        // fly Ray from controller position
       // Ray pointerRay = new Ray (pointer.position, pointer.forward);
        // Starting point of laser
       / * _LaserPointerRenderer.SetPosition (0, pointerRay.origin);
        RaycastHit hitInfo;
        if (Physics.Raycast (pointerRay, out hitInfo, _MaxDistance))
        {
            // Until Ray hit
            // _LaserPointerRenderer.SetPosition (1, hitInfo.point);
        }
        else
        {
            // If Ray doesn't hit, extend MaxDistance in the direction i am facing
            _LaserPointerRenderer.SetPosition (1, pointerRay.origin + pointerRay.direction * _MaxDistance);
        } * /
        if (OVRInput.GetDown (OVRInput.Button.PrimaryIndexTrigger))
        {
            Debug.Log ("shot!");
            Shot (pointer);
        }
        GameObject.Find ("Sword"). Transform.position = pointer.position;
    }
    void Start ()
    {
        Instantiate (Sword, Pointer.position, Quaternion.identity);
    }
}


I think there are two approaches to achieve this goal
The first method is to add a hit judgment to Line Renderer
The second is to link Cube etc. with the controller
The first method does not know the addition of the hit judgment, and the second method feels that it is difficult to know how to link with the controller

Please let me know if anyone can understand

  • Answer # 1

    You should simply place game objects with the following Hierarchy.

    ▼ OVRCameraRig
    ▼ TrackingSpace
    ▼ RightHandaAnchor
    TrackedRemote
    Sword