SpawnAndAttachAfterControllerIsTracking.cs 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  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. using UnityEngine;
  8. using System.Collections;
  9. namespace Valve.VR.InteractionSystem
  10. {
  11. //-------------------------------------------------------------------------
  12. public class SpawnAndAttachAfterControllerIsTracking : MonoBehaviour
  13. {
  14. private Hand hand;
  15. public GameObject itemPrefab;
  16. //-------------------------------------------------
  17. void Start()
  18. {
  19. hand = GetComponentInParent<Hand>();
  20. }
  21. //-------------------------------------------------
  22. void Update()
  23. {
  24. if ( itemPrefab != null )
  25. {
  26. if ( hand.controller != null )
  27. {
  28. if ( hand.controller.hasTracking )
  29. {
  30. GameObject objectToAttach = GameObject.Instantiate( itemPrefab );
  31. objectToAttach.SetActive( true );
  32. hand.AttachObject( objectToAttach );
  33. hand.controller.TriggerHapticPulse( 800 );
  34. Destroy( gameObject );
  35. // If the player's scale has been changed the object to attach will be the wrong size.
  36. // To fix this we change the object's scale back to its original, pre-attach scale.
  37. objectToAttach.transform.localScale = itemPrefab.transform.localScale;
  38. }
  39. }
  40. }
  41. }
  42. }
  43. }