Equippable.cs 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. //======= Copyright (c) Valve Corporation, All rights reserved. ===============
  2. //
  3. // Purpose: Flip Object to match which hand you pick it up in
  4. //
  5. //=============================================================================
  6. using System.Collections;
  7. using System.Collections.Generic;
  8. using UnityEngine;
  9. namespace Valve.VR.InteractionSystem
  10. {
  11. public enum WhichHand
  12. {
  13. Left,
  14. Right
  15. }
  16. [RequireComponent(typeof(Throwable))]
  17. public class Equippable : MonoBehaviour
  18. {
  19. [Tooltip("Array of children you do not want to be mirrored. Text, logos, etc.")]
  20. public Transform[] antiFlip;
  21. public WhichHand defaultHand = WhichHand.Right;
  22. private Vector3 initialScale;
  23. private Interactable interactable;
  24. [HideInInspector]
  25. public SteamVR_Input_Sources attachedHandType
  26. {
  27. get
  28. {
  29. if (interactable.attachedToHand)
  30. return interactable.attachedToHand.handType;
  31. else
  32. return SteamVR_Input_Sources.Any;
  33. }
  34. }
  35. private void Start()
  36. {
  37. initialScale = transform.localScale;
  38. interactable = GetComponent<Interactable>();
  39. }
  40. private void Update()
  41. {
  42. if (interactable.attachedToHand)
  43. {
  44. Vector3 flipScale = initialScale;
  45. if ((attachedHandType == SteamVR_Input_Sources.RightHand && defaultHand == WhichHand.Right) || (attachedHandType == SteamVR_Input_Sources.LeftHand && defaultHand == WhichHand.Left))
  46. {
  47. flipScale.x *= 1;
  48. for (int transformIndex = 0; transformIndex < antiFlip.Length; transformIndex++)
  49. {
  50. antiFlip[transformIndex].localScale = new Vector3(1, 1, 1);
  51. }
  52. }
  53. else
  54. {
  55. flipScale.x *= -1;
  56. for (int transformIndex = 0; transformIndex < antiFlip.Length; transformIndex++)
  57. {
  58. antiFlip[transformIndex].localScale = new Vector3(-1, 1, 1);
  59. }
  60. }
  61. transform.localScale = flipScale;
  62. }
  63. }
  64. }
  65. }