IMHOTEP Framework
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Pages
SpawnAndAttachAfterControllerIsTracking.cs
1 //======= Copyright (c) Valve Corporation, All rights reserved. ===============
2 //
3 // Purpose: Spawns and attaches an object to the hand after the controller has
4 // tracking
5 //
6 //=============================================================================
7 
8 using UnityEngine;
9 using System.Collections;
10 
11 namespace Valve.VR.InteractionSystem
12 {
13  //-------------------------------------------------------------------------
14  public class SpawnAndAttachAfterControllerIsTracking : MonoBehaviour
15  {
16  private Hand hand;
17  public GameObject itemPrefab;
18 
19 
20  //-------------------------------------------------
21  void Start()
22  {
23  hand = GetComponentInParent<Hand>();
24  }
25 
26 
27  //-------------------------------------------------
28  void Update()
29  {
30  if ( itemPrefab != null )
31  {
32  if ( hand.controller != null )
33  {
34  if ( hand.controller.hasTracking )
35  {
36  GameObject objectToAttach = GameObject.Instantiate( itemPrefab );
37  objectToAttach.SetActive( true );
38  hand.AttachObject( objectToAttach );
39  hand.controller.TriggerHapticPulse( 800 );
40  Destroy( gameObject );
41 
42  // If the player's scale has been changed the object to attach will be the wrong size.
43  // To fix this we change the object's scale back to its original, pre-attach scale.
44  objectToAttach.transform.localScale = itemPrefab.transform.localScale;
45  }
46  }
47  }
48  }
49  }
50 }