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.
98 lines
2.7 KiB
98 lines
2.7 KiB
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<Rigidbody>())
|
|
return;
|
|
collidingObject = col.gameObject;
|
|
}
|
|
|
|
//객체 잡기
|
|
private void GrabObject()
|
|
{
|
|
objectInHand = collidingObject; //잡은 객체로 설정
|
|
collidingObject = null; //충돌 객체 해제
|
|
|
|
var joint = AddFixedJoint();
|
|
joint.connectedBody = objectInHand.GetComponent<Rigidbody>();
|
|
}
|
|
//FixedJoint=>객체들을 하나로 묶어 고정시켜줌
|
|
//breakForce=>조인트가 제거되도록 하기 위한 필요한 힘의 크기
|
|
//breakTorque=>조인트가 제거되도록 하기 위한 필요한 토크
|
|
|
|
//joint 추가
|
|
private FixedJoint AddFixedJoint()
|
|
{
|
|
FixedJoint joint = gameObject.AddComponent<FixedJoint>();
|
|
joint.breakForce = 20000;
|
|
joint.breakTorque = 20000;
|
|
return joint;
|
|
}
|
|
//객체 놓기
|
|
// controllerPose.GetVelocity()=>컨트롤러 속도
|
|
// controllerPose.GetAngularVelocity=>컨트롤러 각속도
|
|
/* private void ReleaseObject(){
|
|
if (GetComponent<FixedJoint>()) {
|
|
GetComponent<FixedJoint>().connectedBody = null;
|
|
Destroy(GetComponent<FixedJoint>());
|
|
objectInHand.GetComponent<Rigidbody>().velocity=
|
|
controllerPose.GetVelocity() ;
|
|
objectInHand.GetComponent<Rigidbody>().angularVelocity =
|
|
controllerPose.GetAngularVelocity();
|
|
}
|
|
objectInHand = null;
|
|
}*/
|
|
}
|