Search Unity

switch between object in oculus rift.

Discussion in 'VR' started by mfarhanrosli21, Sep 18, 2019.

  1. mfarhanrosli21

    mfarhanrosli21

    Joined:
    Sep 18, 2019
    Posts:
    2
    Hi guys, im currently trying to figure out how to switch 3d object by triggering it with oculus rift controller.
    Note that im completely noob/new in coding or whatsoever, but i found a tutorial on a youtube that done similar to this.





    I copied his switch character script. Now i know i just have to link its trigger button to oculus controller.
    im pretty sure its really easy but for a complete noob i cant figure it out how to link.

    I figured i should add the input code in line 22.
    If anyone can help, Thanks in advance.



    Here are the codes:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class ToggleItem : MonoBehaviour
    6. {
    7.     // references to controlled game objects
    8.     public GameObject avatar1, avatar2;
    9.    
    10.     // variable contain which avatar is on and active
    11.     int whichAvatarIsOn = 1;
    12.    
    13.     // use this for initialization
    14.     void Start () {
    15.        
    16.         // enable first avatar and disable another one
    17.         avatar1.gameObject.SetActive (true);
    18.         avatar2.gameObject.SetActive (false);
    19.     }
    20.    
    21.     //public method to switch avatar by pressing ui button
    22.     public void Switchavatar()
    23.     {
    24.         //Processing whichavatarIsOn variable
    25.         switch (whichAvatarIsOn) {
    26.        
    27.         //if the first avatar is on
    28.         case 1:
    29.        
    30.            // then the second avatar is on now
    31.            whichAvatarIsOn = 2;
    32.        
    33.            // disable the first one and enable the second one
    34.            avatar1.gameObject.SetActive (false);
    35.            avatar2.gameObject.SetActive (true);
    36.            break;
    37.          
    38.         //if the second avatar is on
    39.         case 2:
    40.        
    41.            //then the first avatar is on now
    42.            whichAvatarIsOn = 1;
    43.          
    44.            //disable the second one and enable the first one
    45.            avatar1.gameObject.SetActive (true);
    46.            avatar2.gameObject.SetActive (false);
    47.            break;
    48.          
    49.         }
    50.                
    51.     }  
    52. }