Ok, so I managed to be able to “snap” it into place, but I wanted it to smoothly lerp into place when the player lets go. I took the code from the Vector3.Lerp example and tried to apply it to a colider in the place of a slot:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using VRTK;
public class TestLerpSlot : MonoBehaviour {
public Transform startMarker;
public Transform endMarker;
public float speed = 1.0F;
private float startTime;
private float journeyLength;
private VRTK_InteractableObject interactable;
private Rigidbody rb;
// Use this for initialization
void Start ()
{
startTime = Time.time;
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider colider)
{
if (colider.tag == (“1U Server”))
{
rb = colider.gameObject.GetComponent();
if (colider.gameObject.GetComponent<VRTK_InteractableObject>())
{
interactable = colider.gameObject.GetComponent<VRTK_InteractableObject>();
if (interactable.IsGrabbed())
{
Debug.Log(“It’s Grabbed”);
}
//if (interactable.IsGrabbed() == false )
if (!interactable.IsGrabbed())
{
rb.isKinematic = true;
//Transform startMarker = colider.transform;
journeyLength = Vector3.Distance(startMarker.position, endMarker.position);
float distCovered = (Time.time - startTime) * speed;
float fracJourney = distCovered / journeyLength;
colider.transform.position = Vector3.Lerp(startMarker.position, endMarker.position, fracJourney);
colider.transform.rotation = Quaternion.Lerp(startMarker.rotation, endMarker.rotation, Time.time * speed);
Debug.Log(“It’s not Grabbed”);
}
}
Debug.Log(“Hit Slot”);
}
}
void OnTriggerExit(Collider colider)
{
if (colider.tag == (“1U Server”))
{
rb = colider.gameObject.GetComponent();
rb.isKinematic = false;
Debug.LogError(“Kine false”);
}
}
}
There are several problems with this:
- It’s not lerping the object, just snapping it to the place
- If I use OnTriggerStay instead it won’t let go
- It’s not setting kinematic to false when I want to take it out of the slot
- I have to manually assign the transform of the slot position (“endMarker”) even though this script is attached to the slot object
- I’d like the startMarker to be the colliding object’s transform at the moment the player lets go (or where it hits if it’s thrown at it.)
I want it to be like this if you get it close enough to the rails: https://media.giphy.com/media/qTNIlLJNOwdNK/giphy.gif