- Home /
Picking up object, then locking it back into same position "Oculus VR"
Hi,
I'm using OVR Grabbable component to pick up a "Cube" for example, moving it around in front of the Oculus headset. I would like to be able to put the "Cube" back in the location I picked it up from, almost as if the object locks back into place / magnetic.
Could anyone help me with a script or explain how to do it, I'm really struggling.
Any help would be greatly appreciated.
Store the desired position and rotation of the object (for example in a SavePosition()
function), and let it have a bool isHeld
variable which you set to true
when holding the item, and false
when not. Finally, in Update()
if isHeld
is false
lerp back the position and rotation to the one you saved the in SavePosition()
.
Hi, Is that using a C## script? if so could you please help me? I'm unsure on how to write it.
Thanks,
As UnityScript support is being ended, and Boo hasn't been supported for a long while now, yes, it is C#.
Try the below outline of a script. Call SaveReturnPoint()
and SetIsHeld(true)
when you take the object, and just SetIsHeld(false)
when you let go of it.
public class ReturningObject : $$anonymous$$onoBehaviour {
private Vector3 returnPosition;
private Quaternion returnRotation;
private bool isHeld = false;
public void SetIsHeld (bool value) { isHeld = value; }
public void SaveReturnPoint () {
returnPosition = transform.position;
returnRotation = transform.rotation;
}
private void Update () {
if (!isHeld) {
transform.position = Vector3.Lerp(transform.position, returnPosition, 0.5f);
transform.rotation = Quaternion.Lerp(transform.rotation, returnRotation, 0.5f);
}
}
}
Answer by RedMoonGms · Jul 02, 2018 at 03:23 PM
Maybe you could use Input.GetKeyUp and make an animation of the object getting back to position.
void Update() { if (Input.GetKeyUp("Action")) TheObject.GetComponent<Animation> ().Play ("ObjectBackToPlace");
This way the object should get back as you stop pressing a certain key. I'm not very well at scripting so, I'm not sure this could work. Hope this helps anyway.
BTW, this script should be activated only when you are holding the object.