Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

//NEED HELP// Vive Controller as animation trigger

Discussion in 'AR/VR (XR) Discussion' started by manuelhess0, Oct 22, 2018.

  1. manuelhess0

    manuelhess0

    Joined:
    Aug 16, 2018
    Posts:
    1
    Hi guys,

    I'm an absolute beginner in C# so this is probably a very basic problem to you.
    What I'm trying to achieve is to use a Vive's trigger button to start an animation. I tried this little script
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class animController : MonoBehaviour {
    5.  
    6.     public Animator anim;
    7.  
    8.  
    9.     void Start () {
    10.  
    11.         anim = GetComponent<Animator>();
    12.  
    13.     }
    14.    
    15.     void Update () {
    16.  
    17.         if (Input.GetKeyDown("a"))
    18.         {
    19.             anim.Play("animate_display");
    20.         }
    21.     }
    22. }
    23.  
    24.  
    25.  
    with only keyboard input, which worked just fine. Altering it for the Controller input like this:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using Valve.VR;
    5.  
    6. public class animController : MonoBehaviour {
    7.  
    8.     public Animator anim;
    9.     public SteamVR_TrackedObject mTrackedObject = null;
    10.     public SteamVR_Controller.Device mDevice;
    11.  
    12.     void Awake()
    13.     {
    14.         mTrackedObject = GetComponent<SteamVR_TrackedObject>();
    15.     }
    16.  
    17.     void Start () {
    18.  
    19.         anim = GetComponent<Animator>();
    20.  
    21.     }
    22.    
    23.  
    24.     void Update () {
    25.  
    26.         mDevice = SteamVR_Controller.Input((int)mTrackedObject.index);
    27.  
    28.         #region Trigger
    29.    
    30.         if (mDevice.GetTouchDown(SteamVR_Controller.ButtonMask.Trigger))
    31.         {
    32.             anim.Play("animate_display");
    33.         }
    34.         #endregion
    35.     }
    36. }
    37.  
    Only leaves me with the following error:

    NullReferenceException: Object reference not set to an instance of an object
    animController.Update () (at Assets/animController.cs:27)

    and empting both these fields, Anim and Tracked object, as soon as I hit Play. upload_2018-10-22_16-3-0.png

    Reassigning them during Playmode gets it working.

    I have no idea why that is and I'm sure there's a better solution to this.
    Thanks for your help
     
  2. unity_9sduCg4b-HIz3w

    unity_9sduCg4b-HIz3w

    Joined:
    Jun 13, 2019
    Posts:
    1
    Hi,
    have you found a solution to this? i would also like to do something like that and would be very grateful if you could let me know how you did it.
    regards,
    charleen