using System.Collections; using System.Collections.Generic; using UnityEngine; using Valve.VR; public class grabInteraction : MonoBehaviour { public SteamVR_Input_Sources handType; //¸ðµÎ »ç¿ë, ¿Þ¼Õ, ¿À¸¥¼Õ public SteamVR_Behaviour_Pose controllerPose; // ÄÁÆ®·Ñ·¯ Á¤º¸ public SteamVR_Action_Boolean grabAction; //±×¸³ ¾×¼Ç private GameObject collidingObject; //ÇöÀç Ãæµ¹ÁßÀÎ °´Ã¼ private GameObject objectInHand; //Ç÷¹À̾ ÀâÀº °´Ã¼ private void Update() { //trigger ¹öưÀ» ´©¸¦ ¶§ if (grabAction.GetLastStateDown(handType)) { if (collidingObject) { GrabObject(); } } //trigger ¹öưÀ» ³õÀ» ¶§ /* if (grabAction.GetLastStateDown(handType)) { if (objectInHand) { GrabObject(); } }*/ } // Ãæµ¹ÀÌ ½ÃÀ۵Ǵ ¼ø°£ public void OnTriggerEnter(Collider other) { SetCollidingObject(other); } // Ãæµ¹ ÁßÀÏ ¶§ public void OnTriggerStay(Collider other) { SetCollidingObject(other); } // Ãæµ¹ÀÌ ³¡³ª´Â ¼ø°£ public void OnTriggerExit(Collider other) { if (!collidingObject) return; collidingObject = null; } //Ãæµ¹ ÁßÀÎ °´Ã¼·Î ¼³Á¤ private void SetCollidingObject(Collider col) { //ÀÌ¹Ì Ãæµ¹ ÁßÀ̰ųª rigidbody¸¦ °¡Áö°í ÀÖÁö ¾ÊÀº °æ¿ì ¿¹¿Üó¸® if (collidingObject || !col.GetComponent()) return; collidingObject = col.gameObject; } //°´Ã¼ Àâ±â private void GrabObject() { objectInHand = collidingObject; //ÀâÀº °´Ã¼·Î ¼³Á¤ collidingObject = null; //Ãæµ¹ °´Ã¼ ÇØÁ¦ var joint = AddFixedJoint(); joint.connectedBody = objectInHand.GetComponent(); } //FixedJoint=>°´Ã¼µéÀ» Çϳª·Î ¹­¾î °íÁ¤½ÃÄÑÁÜ //breakForce=>Á¶ÀÎÆ®°¡ Á¦°ÅµÇµµ·Ï Çϱâ À§ÇÑ ÇÊ¿äÇÑ ÈûÀÇ Å©±â //breakTorque=>Á¶ÀÎÆ®°¡ Á¦°ÅµÇµµ·Ï Çϱâ À§ÇÑ ÇÊ¿äÇÑ ÅäÅ© //joint Ãß°¡ private FixedJoint AddFixedJoint() { FixedJoint joint = gameObject.AddComponent(); joint.breakForce = 20000; joint.breakTorque = 20000; return joint; } //°´Ã¼ ³õ±â // controllerPose.GetVelocity()=>ÄÁÆ®·Ñ·¯ ¼Óµµ // controllerPose.GetAngularVelocity=>ÄÁÆ®·Ñ·¯ °¢¼Óµµ /* private void ReleaseObject(){ if (GetComponent()) { GetComponent().connectedBody = null; Destroy(GetComponent()); objectInHand.GetComponent().velocity= controllerPose.GetVelocity() ; objectInHand.GetComponent().angularVelocity = controllerPose.GetAngularVelocity(); } objectInHand = null; }*/ }