using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using VRTK;
|
|
|
|
public class ReturnToStart : MonoBehaviour
|
|
{
|
|
|
|
private Vector3 m_StartPos;
|
|
private Quaternion m_StartRot;
|
|
private VRTK_InteractableObject m_interactable;
|
|
private Rigidbody m_rigid;
|
|
|
|
|
|
// Start is called before the first frame update
|
|
void Awake()
|
|
{
|
|
m_StartPos = transform.position;
|
|
m_StartRot = transform.rotation;
|
|
//Set up some variables on start
|
|
m_interactable = GetComponent<VRTK_InteractableObject>();
|
|
m_rigid = GetComponentInChildren<Rigidbody>();
|
|
}
|
|
|
|
private void OnEnable()
|
|
{
|
|
RegisterEvents(true);
|
|
}
|
|
|
|
|
|
private void OnDisable()
|
|
{
|
|
RegisterEvents(false);
|
|
}
|
|
|
|
private void OnGrab(object sender, InteractableObjectEventArgs args)
|
|
{
|
|
StopAllCoroutines();
|
|
m_rigid.isKinematic = false;
|
|
}
|
|
|
|
|
|
private void OnDrop(object sender, InteractableObjectEventArgs args)
|
|
{
|
|
//other hand might still be grabbed
|
|
if (!m_interactable.IsGrabbed())
|
|
{
|
|
StopAllCoroutines();
|
|
StartCoroutine(LerpToPosition(m_StartPos, m_StartRot, 2.0f));
|
|
}
|
|
}
|
|
|
|
|
|
private void OnSnap(object sender, InteractableObjectEventArgs args)
|
|
{
|
|
StopAllCoroutines();
|
|
//m_rigid.isKinematic = true;
|
|
}
|
|
|
|
|
|
private void RegisterEvents(bool value)
|
|
{
|
|
if (value)
|
|
{
|
|
m_interactable.InteractableObjectSnappedToDropZone += OnSnap;
|
|
m_interactable.InteractableObjectGrabbed += OnGrab;
|
|
m_interactable.InteractableObjectUngrabbed += OnDrop;
|
|
}
|
|
else
|
|
{
|
|
m_interactable.InteractableObjectSnappedToDropZone -= OnSnap;
|
|
m_interactable.InteractableObjectGrabbed -= OnGrab;
|
|
m_interactable.InteractableObjectUngrabbed -= OnDrop;
|
|
}
|
|
}
|
|
|
|
#region Coroutines
|
|
/// <summary>
|
|
/// Lerps object to a position over an amount of time
|
|
/// </summary>
|
|
/// <param name="SnapPos">End position</param>
|
|
/// <param name="SnapRot">End rotation</param>
|
|
/// <param name="snapTime">Time it takes to lerp</param>
|
|
private IEnumerator LerpToPosition(Vector3 SnapPos, Quaternion SnapRot, float snapTime)
|
|
{
|
|
Debug.Log("Lerp to position");
|
|
//This is based off of SteamVR Lerping code
|
|
float dropTimer = -1;
|
|
m_rigid.isKinematic = false;
|
|
|
|
|
|
//once again this is all steamVR
|
|
while (dropTimer < 1)
|
|
{
|
|
float t = Mathf.Pow(35, dropTimer);
|
|
|
|
m_rigid.velocity = Vector3.Lerp(m_rigid.velocity, Vector3.zero, Time.fixedDeltaTime * 4);
|
|
if (m_rigid.useGravity)
|
|
m_rigid.AddForce(-Physics.gravity);
|
|
|
|
transform.position = Vector3.Lerp(transform.position, SnapPos, Time.fixedDeltaTime * t * 3);
|
|
transform.rotation = Quaternion.Slerp(transform.rotation, SnapRot, Time.fixedDeltaTime * t * 5);
|
|
|
|
yield return new WaitForFixedUpdate();
|
|
dropTimer += Time.fixedDeltaTime / (snapTime / 2);
|
|
}
|
|
|
|
//#Audio: object has just arrived at where it was lerping to
|
|
|
|
//set correct transform in case object stuck
|
|
m_rigid.isKinematic = true;
|
|
transform.position = SnapPos;
|
|
transform.rotation = SnapRot;
|
|
}
|
|
#endregion Coroutines
|
|
|
|
}
|