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.
44 lines
1.6 KiB
44 lines
1.6 KiB
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.XR.Interaction.Toolkit;
|
|
|
|
public class XROffsetGrabInteractable : XRGrabInteractable
|
|
{
|
|
//Pivot을 리셋시키기 위해 원래 로컬 위치, 회전 저장
|
|
private Vector3 initialAttachLocalPos;
|
|
private Quaternion initialAttachLocalRot;
|
|
|
|
protected virtual void Start()
|
|
{
|
|
//attachTransform 이 없다면 만들어준다.
|
|
if(!attachTransform)
|
|
{
|
|
GameObject pivot = new GameObject("Attach Pivot"); //피봇용 빈 게임오브젝트를 만들어서
|
|
pivot.transform.SetParent(transform, false); //자신의 자식으로 넣고, 위치를 0, 0, 0 으로
|
|
attachTransform = pivot.transform;
|
|
}
|
|
|
|
initialAttachLocalPos = attachTransform.localPosition;
|
|
initialAttachLocalRot = attachTransform.localRotation;
|
|
}
|
|
|
|
//잡는 순간 실행되는 함수 오버라이드
|
|
protected override void OnSelectEntering(XRBaseInteractor interactor)
|
|
{
|
|
//직접 손으로 잡으면 Offset Grabbing
|
|
if(interactor is XRDirectInteractor)
|
|
{
|
|
attachTransform.position = interactor.attachTransform.position;
|
|
attachTransform.rotation = interactor.attachTransform.rotation;
|
|
}
|
|
else
|
|
{
|
|
//그 외에는 Pivot값을 원래대로 리셋시켜줌
|
|
attachTransform.localPosition = initialAttachLocalPos;
|
|
attachTransform.localRotation = initialAttachLocalRot;
|
|
}
|
|
|
|
base.OnSelectEntering(interactor);
|
|
}
|
|
}
|