SpawnAndAttachAfterControllerIsTracking.cs 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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.isActive && hand.isPoseValid)
  27. {
  28. GameObject objectToAttach = GameObject.Instantiate(itemPrefab);
  29. objectToAttach.SetActive(true);
  30. hand.AttachObject(objectToAttach, GrabTypes.Scripted);
  31. hand.TriggerHapticPulse(800);
  32. Destroy(gameObject);
  33. // If the player's scale has been changed the object to attach will be the wrong size.
  34. // To fix this we change the object's scale back to its original, pre-attach scale.
  35. objectToAttach.transform.localScale = itemPrefab.transform.localScale;
  36. }
  37. }
  38. }
  39. }
  40. }