Search Unity

How to make gameobject disappear on controller touch? (script included)

Discussion in 'AR/VR (XR) Discussion' started by felt9099, May 5, 2020.

  1. felt9099

    felt9099

    Joined:
    Feb 1, 2018
    Posts:
    36
    Hey friends,

    I'm making a Unity VR scavenger hunt where the player goes around the environment and grabs "presents" (little gift boxes). Currently, the player walks over the gift (with thumbpad presses) and it disappears. But I'd rather them "touch" the present with their hand-held controller to make the object disappear, which is a bit more realistic.

    I've attached this script to the Right Hand of the Steam VR Player Prefab, but it's not working. I can walk over the presents, but cannot touch them to make them disappear... Any ideas on what I'm missing?

    Thanks!

    - Will

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class PresentCounter : MonoBehaviour
    7. {
    8.     // Start is called before the first frame update
    9.  
    10.     private int count;
    11.     //public Text countText;
    12.     public TextMesh countText;
    13.     public TextMesh countText1;
    14.  
    15.     // I have two scoreboards in the environment, which is what the CountTexts are referring to.
    16.    
    17.     void Start()
    18.     {
    19.         count = 0;
    20.         SetCountText();
    21.     }
    22.  
    23.     // Update is called once per frame
    24.     void Update()
    25.     {
    26.  
    27.     }
    28.  
    29.     private void OnTriggerEnter(Collider other)
    30.     {
    31.         {
    32.             if (other.gameObject.CompareTag("Present"))
    33.             {
    34.                 other.gameObject.SetActive(false);
    35.                 Debug.Log(other.gameObject.name);
    36.                 count = count + 1;
    37.                 SetCountText();
    38.                 // So, I think what I'm saying here is: if the GameObject that this script is attached to (the Right Hand Controller) touches an object tagged "Present"
    39.                 // Then the object is set inactive, the name is logged, and the count goes up by one.
    40.             }
    41.         }
    42.     }
    43.  
    44.     void SetCountText()
    45.     {
    46.         //countText.text = "Count: " + count.ToString();
    47.         countText.text = "Your Score: " + count.ToString();
    48.         countText1.text = "Your Score: " + count.ToString();
    49.  
    50.     }
    51. }
    52.  
     
    Dark-Sniper101 likes this.
  2. felt9099

    felt9099

    Joined:
    Feb 1, 2018
    Posts:
    36
    I found a solution.

    For anyone who discovers this thread and has a similar problem later:
    • I removed the Body Collider from the SteamVR prefab Player object
    • I added a small sphere object to the game and had it follow the position of the right hand controller
    • I attached the above script to the sphere - and it now works as intended.