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

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<Rigidbody>())
return;
collidingObject = col.gameObject;
}
//잡기
private void ShootingObject()
{
objectInHand = collidingObject;
collidingObject = null;
var joint = AddFixedJoint();
joint.connectedBody = objectInHand.GetComponent<Rigidbody>();
}
//joint 추가
private FixedJoint AddFixedJoint()
{
FixedJoint joint = gameObject.AddComponent<FixedJoint>();
joint.breakForce = 20000;
joint.breakTorque = 20000;
return joint;
}
//놓기
private void RelaseObject()
{
if (GetComponent<FixedJoint>())
{
GetComponent<FixedJoint>().connectedBody = null;
Destroy(GetComponent<FixedJoint>());
objectInHand.GetComponent<Rigidbody>().velocity =
controllerPose.GetVelocity();
objectInHand.GetComponent<Rigidbody>().angularVelocity =
controllerPose.GetAngularVelocity();
}
objectInHand = null;
}
}