Search Unity

[EDIT] Switch camera on hover an object

Discussion in 'AR/VR (XR) Discussion' started by tibewww, Feb 3, 2016.

  1. tibewww

    tibewww

    Joined:
    Jan 27, 2016
    Posts:
    5
    Hi Everybody,

    Here is my situation:

    I'd like to have the possiblity to switch camera when the user hover an object

    I am using VR Eye Caster,Vr Input,etc to be able to focus object inside that environment and interact with them.

    so far I am using this code which change material on hover the object, But i would like isntead to switch camera:

    Code (CSharp):
    1. using System;
    2. using UnityEngine;
    3.  
    4. namespace VRStandardAssets.Utils
    5. {
    6.     // This class should be added to any gameobject in the scene
    7.     // that should react to input based on the user's gaze.
    8.     // It contains events that can be subscribed to by classes that
    9.     // need to know about input specifics to this gameobject.
    10.     public class VRInteractiveItem : MonoBehaviour
    11.     {
    12.         public event Action OnOver;             // Called when the gaze moves over this object
    13.         public event Action OnOut;              // Called when the gaze leaves this object
    14.         public event Action OnClick;            // Called when click input is detected whilst the gaze is over this object.
    15.         public event Action OnDoubleClick;      // Called when double click input is detected whilst the gaze is over this object.
    16.         public event Action OnUp;               // Called when Fire1 is released whilst the gaze is over this object.
    17.         public event Action OnDown;             // Called when Fire1 is pressed whilst the gaze is over this object.
    18.  
    19.  
    20.         protected bool m_IsOver;
    21.  
    22.  
    23.         public bool IsOver
    24.         {
    25.             get { return m_IsOver; }              // Is the gaze currently over this object?
    26.         }
    27.  
    28.  
    29.         // The below functions are called by the VREyeRaycaster when the appropriate input is detected.
    30.         // They in turn call the appropriate events should they have subscribers.
    31.         public void Over()
    32.         {
    33.             m_IsOver = true;
    34.  
    35.             if (OnOver != null)
    36.                 OnOver();
    37.         }
    38.  
    39.  
    40.         public void Out()
    41.         {
    42.             m_IsOver = false;
    43.  
    44.             if (OnOut != null)
    45.                 OnOut();
    46.         }
    47.  
    48.  
    49.         public void Click()
    50.         {
    51.             if (OnClick != null)
    52.                 OnClick();
    53.         }
    54.  
    55.  
    56.         public void DoubleClick()
    57.         {
    58.             if (OnDoubleClick != null)
    59.                 OnDoubleClick();
    60.         }
    61.  
    62.  
    63.         public void Up()
    64.         {
    65.             if (OnUp != null)
    66.                 OnUp();
    67.         }
    68.  
    69.  
    70.         public void Down()
    71.         {
    72.             if (OnDown != null)
    73.                 OnDown();
    74.         }
    75.     }
    76. }
    Code (CSharp):
    1. using UnityEngine;
    2. using VRStandardAssets.Utils;
    3.  
    4. namespace VRStandardAssets.Examples
    5. {
    6.     // This script is a simple example of how an interactive item can
    7.     // be used to change things on gameobjects by handling events.
    8.     public class ExampleInteractiveItem : MonoBehaviour
    9.     {
    10.         [SerializeField] private Material m_NormalMaterial;              
    11.         [SerializeField] private Material m_OverMaterial;                
    12.         [SerializeField] private Material m_ClickedMaterial;            
    13.         [SerializeField] private Material m_DoubleClickedMaterial;      
    14.         [SerializeField] private VRInteractiveItem m_InteractiveItem;
    15.         [SerializeField] private Renderer m_Renderer;
    16.  
    17.  
    18.         private void Awake ()
    19.         {
    20.             m_Renderer.material = m_NormalMaterial;
    21.         }
    22.  
    23.  
    24.         private void OnEnable()
    25.         {
    26.             m_InteractiveItem.OnOver += HandleOver;
    27.             m_InteractiveItem.OnOut += HandleOut;
    28.             m_InteractiveItem.OnClick += HandleClick;
    29.             m_InteractiveItem.OnDoubleClick += HandleDoubleClick;
    30.         }
    31.  
    32.  
    33.         private void OnDisable()
    34.         {
    35.             m_InteractiveItem.OnOver -= HandleOver;
    36.             m_InteractiveItem.OnOut -= HandleOut;
    37.             m_InteractiveItem.OnClick -= HandleClick;
    38.             m_InteractiveItem.OnDoubleClick -= HandleDoubleClick;
    39.         }
    40.  
    41.  
    42.         //Handle the Over event
    43.         private void HandleOver()
    44.         {
    45.             Debug.Log("Show over state");
    46.             m_Renderer.material = m_OverMaterial;
    47.         }
    48.  
    49.  
    50.         //Handle the Out event
    51.         private void HandleOut()
    52.         {
    53.             Debug.Log("Show out state");
    54.             m_Renderer.material = m_NormalMaterial;
    55.         }
    56.  
    57.  
    58.         //Handle the Click event
    59.         private void HandleClick()
    60.         {
    61.             Debug.Log("Show click state");
    62.             m_Renderer.material = m_ClickedMaterial;
    63.         }
    64.  
    65.  
    66.         //Handle the DoubleClick event
    67.         private void HandleDoubleClick()
    68.         {
    69.             Debug.Log("Show double click");
    70.             m_Renderer.material = m_DoubleClickedMaterial;
    71.         }
    72.     }
    73.  
    74. }
    How is this possible to achieve ? Does anybody already use this ?

    Thank you for your help guys
     
    Last edited: Feb 3, 2016