2021년 4학년 1학기 기업연계프로젝트2 컴퓨터소프트웨어공학과 <원광투어팀> 팀장 : 송유진 팀원 : 김나영, 이경희, 한유진
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

94 lines
2.3 KiB

5 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Valve.VR;
  5. public class shootingInteraction : MonoBehaviour
  6. {
  7. public SteamVR_Input_Sources handType;
  8. public SteamVR_Behaviour_Pose controllerPose; // controller info
  9. public SteamVR_Action_Boolean shootingAction;
  10. private GameObject collidingObject;
  11. private GameObject objectInHand;
  12. private void Update()
  13. {
  14. //trackpad ��ư�� ���� ��
  15. if (shootingAction.GetLastStateDown(handType))
  16. {
  17. if (collidingObject)
  18. {
  19. ShootingObject();
  20. }
  21. }
  22. //trackpad ��ư�� ���� ��
  23. if (shootingAction.GetLastStateUp(handType))
  24. {
  25. if (objectInHand)
  26. {
  27. RelaseObject();
  28. }
  29. }
  30. }
  31. public void OnTrackpadEnter(Collider other)
  32. {
  33. SetCollidingObject(other);
  34. }
  35. public void OnTrackpadStay(Collider other)
  36. {
  37. SetCollidingObject(other);
  38. }
  39. public void OnTrackpadExit(Collider other)
  40. {
  41. if (!collidingObject)
  42. return;
  43. collidingObject = null;
  44. }
  45. //�浹 ���� ��ü�� üũ
  46. private void SetCollidingObject(Collider col)
  47. {
  48. //�̹� �浹 ���̰ų� rigidbody�� ������ ���� ���� ���� ����ó��
  49. if (collidingObject || !col.GetComponent<Rigidbody>())
  50. return;
  51. collidingObject = col.gameObject;
  52. }
  53. //����
  54. private void ShootingObject()
  55. {
  56. objectInHand = collidingObject;
  57. collidingObject = null;
  58. var joint = AddFixedJoint();
  59. joint.connectedBody = objectInHand.GetComponent<Rigidbody>();
  60. }
  61. //joint �߰�
  62. private FixedJoint AddFixedJoint()
  63. {
  64. FixedJoint joint = gameObject.AddComponent<FixedJoint>();
  65. joint.breakForce = 20000;
  66. joint.breakTorque = 20000;
  67. return joint;
  68. }
  69. //����
  70. private void RelaseObject()
  71. {
  72. if (GetComponent<FixedJoint>())
  73. {
  74. GetComponent<FixedJoint>().connectedBody = null;
  75. Destroy(GetComponent<FixedJoint>());
  76. objectInHand.GetComponent<Rigidbody>().velocity =
  77. controllerPose.GetVelocity();
  78. objectInHand.GetComponent<Rigidbody>().angularVelocity =
  79. controllerPose.GetAngularVelocity();
  80. }
  81. objectInHand = null;
  82. }
  83. }