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.

97 lines
2.7 KiB

5 years ago
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using Valve.VR;
  5. public class grabInteraction : MonoBehaviour
  6. {
  7. public SteamVR_Input_Sources handType; //���� ����, �޼�, ������
  8. public SteamVR_Behaviour_Pose controllerPose; // ��Ʈ�ѷ� ����
  9. public SteamVR_Action_Boolean grabAction; //�׸� �׼�
  10. private GameObject collidingObject; //���� �浹���� ��ü
  11. private GameObject objectInHand; //�÷��̾ ���� ��ü
  12. private void Update()
  13. {
  14. //trigger ��ư�� ���� ��
  15. if (grabAction.GetLastStateDown(handType))
  16. {
  17. if (collidingObject)
  18. {
  19. GrabObject();
  20. }
  21. }
  22. //trigger ��ư�� ���� ��
  23. /* if (grabAction.GetLastStateDown(handType))
  24. {
  25. if (objectInHand)
  26. {
  27. GrabObject();
  28. }
  29. }*/
  30. }
  31. // �浹�� ���۵Ǵ� ����
  32. public void OnTriggerEnter(Collider other)
  33. {
  34. SetCollidingObject(other);
  35. }
  36. // �浹 ���� ��
  37. public void OnTriggerStay(Collider other)
  38. {
  39. SetCollidingObject(other);
  40. }
  41. // �浹�� ������ ����
  42. public void OnTriggerExit(Collider other)
  43. {
  44. if (!collidingObject)
  45. return;
  46. collidingObject = null;
  47. }
  48. //�浹 ���� ��ü�� ����
  49. private void SetCollidingObject(Collider col)
  50. {
  51. //�̹� �浹 ���̰ų� rigidbody�� ������ ���� ���� ���� ����ó��
  52. if (collidingObject || !col.GetComponent<Rigidbody>())
  53. return;
  54. collidingObject = col.gameObject;
  55. }
  56. //��ü ����
  57. private void GrabObject()
  58. {
  59. objectInHand = collidingObject; //���� ��ü�� ����
  60. collidingObject = null; //�浹 ��ü ����
  61. var joint = AddFixedJoint();
  62. joint.connectedBody = objectInHand.GetComponent<Rigidbody>();
  63. }
  64. //FixedJoint=>��ü���� �ϳ��� ���� ����������
  65. //breakForce=>����Ʈ�� ���ŵǵ��� �ϱ� ���� �ʿ��� ���� ũ��
  66. //breakTorque=>����Ʈ�� ���ŵǵ��� �ϱ� ���� �ʿ��� ��ũ
  67. //joint �߰�
  68. private FixedJoint AddFixedJoint()
  69. {
  70. FixedJoint joint = gameObject.AddComponent<FixedJoint>();
  71. joint.breakForce = 20000;
  72. joint.breakTorque = 20000;
  73. return joint;
  74. }
  75. //��ü ����
  76. // controllerPose.GetVelocity()=>��Ʈ�ѷ� �ӵ�
  77. // controllerPose.GetAngularVelocity=>��Ʈ�ѷ� ���ӵ�
  78. /* private void ReleaseObject(){
  79. if (GetComponent<FixedJoint>()) {
  80. GetComponent<FixedJoint>().connectedBody = null;
  81. Destroy(GetComponent<FixedJoint>());
  82. objectInHand.GetComponent<Rigidbody>().velocity=
  83. controllerPose.GetVelocity() ;
  84. objectInHand.GetComponent<Rigidbody>().angularVelocity =
  85. controllerPose.GetAngularVelocity();
  86. }
  87. objectInHand = null;
  88. }*/
  89. }