using System.Collections; using System.Collections.Generic; using UnityEngine; using Valve.VR; public class shootingInteraction : MonoBehaviour { public SteamVR_Input_Sources handType; public SteamVR_Behaviour_Pose controllerPose; // controller info public SteamVR_Action_Boolean shootingAction; private GameObject collidingObject; private GameObject objectInHand; private void Update() { //trackpad ¹öưÀ» ´©¸¦ ¶§ if (shootingAction.GetLastStateDown(handType)) { if (collidingObject) { ShootingObject(); } } //trackpad ¹öưÀ» ³õÀ» ¶§ if (shootingAction.GetLastStateUp(handType)) { if (objectInHand) { RelaseObject(); } } } public void OnTrackpadEnter(Collider other) { SetCollidingObject(other); } public void OnTrackpadStay(Collider other) { SetCollidingObject(other); } public void OnTrackpadExit(Collider other) { if (!collidingObject) return; collidingObject = null; } //Ãæµ¹ ÁßÀÎ °³Ã¼·Î üũ private void SetCollidingObject(Collider col) { //ÀÌ¹Ì Ãæµ¹ ÁßÀ̰ųª rigidbody¸¦ °¡Áö°í ÀÖÁö ¾ÊÀº °æ¿ì ¿¹¿Üó¸® if (collidingObject || !col.GetComponent()) return; collidingObject = col.gameObject; } //Àâ±â private void ShootingObject() { objectInHand = collidingObject; collidingObject = null; var joint = AddFixedJoint(); joint.connectedBody = objectInHand.GetComponent(); } //joint Ãß°¡ private FixedJoint AddFixedJoint() { FixedJoint joint = gameObject.AddComponent(); joint.breakForce = 20000; joint.breakTorque = 20000; return joint; } //³õ±â private void RelaseObject() { if (GetComponent()) { GetComponent().connectedBody = null; Destroy(GetComponent()); objectInHand.GetComponent().velocity = controllerPose.GetVelocity(); objectInHand.GetComponent().angularVelocity = controllerPose.GetAngularVelocity(); } objectInHand = null; } }