Aishwarya Dang
Aishwarya Dang

Reputation: 11

Unity 3d VR - Hide and Show Model on Controller Button Click

I am beginning VR development and am creating a basic VR app where I put 2 custom 3d models in a scene. Let first model be 'a' and second be 'b' I would like to show 'a' and then when someone presses some key on the oculus controller, I would like to hide 'a' and Show 'b'. How can I do it? I understand that the keydown/keyup function will be used. I'd like to know how to hide/inside the model.

Upvotes: 1

Views: 1158

Answers (2)

Ohad Cohen
Ohad Cohen

Reputation: 6144

If you don't want to use Oculus specific dependencies (such as OVR) you can use UnityEngine.XR.InputDevice which should be generic

List<InputDevice> devices = new List<InputDevice>();
InputDevices.GetDevicesWithCharacteristics(InputDeviceCharacteristics.Controller |
        InputDeviceCharacteristics.TrackedDevice |  InputDeviceCharacteristics.Right,
        devices);
foreach (var item in devices)
{
    List<InputFeatureUsage> l = new List<InputFeatureUsage>();
    item.TryGetFeatureUsages(l);
    bool resBool;

    foreach (var i in l)
    {
        // I guess there could be a more elegant solution here.
        if (i.name == "TriggerButton")
        {
            item.TryGetFeatureValue(i.As<bool>(), out resBool);
            if (resBool) {
                modelA.GetComponent<Renderer>().enabled = true;
                modelB.GetComponent<Renderer>().enabled = false;
            } else {
                modelA.GetComponent<Renderer>().enabled = false;
                modelB.GetComponent<Renderer>().enabled = true;
            }
        }
    }
}

Upvotes: 0

Programmer
Programmer

Reputation: 125275

To hide the GameObject, use the SetActive function and pass true/false to show/hide it. The is activates and de-activates the GameObject:

public GameObject modelA;
public GameObject modelB;

void Update()
{
    OVRInput.Update(); 

    if (OVRInput.Get(OVRInput.Button.One))
    {
        //Hide model A
        modelA.SetActive(false);

        //Show model B
        modelB.SetActive(true);
    }
}

If you don't want to activate/de-activate the GameObject, just enable/disable the MeshRenderer component:

public GameObject modelA;
public GameObject modelB;

void Update()
{
    OVRInput.Update(); 

    if (OVRInput.Get(OVRInput.Button.One))
    {
        //Hide model A
        modelA.GetComponent<MeshRenderer>().enabled = false;

        //Show model B
        modelB.GetComponent<MeshRenderer>().enabled = true;
    }
}

Upvotes: 1

Related Questions