cancel
Showing results for 
Search instead for 
Did you mean: 

How to returning a gameobject to its original position when release a grab from touch controller.

VJ76
Level 4
I'm having a scene with multiple gameobjects that i can grab. I am using the OVR Grabber and Grabbable scripts, but I want to set it up so that when the player releases the object in a certain distance, it snaps back to it's original position and rotation. This without using vrtk please
8 REPLIES 8

Corysia
Level 5
Save your original position and rotation on-start.
When the object is released, if the threshhold value for the distance is reached, re-position and rotate based on your saved values.

float dist = Vector3.Distance(other.position, transform.position); // calc the distance

VJ76
Level 4
@Corysia  Thx for your answer!
I have created two scripts, one where i exposed the OnGrabBegin and OnGrabEnd in the OVRGrabbable.cs and one for the grabbed part: But so far it doesnt work, what am i missing here? 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;
 
public class OVRGrabbableExtended : OVRGrabbable
{
    [HideInInspector] public UnityEvent OnGrabBegin;
    [HideInInspector] public UnityEvent OnGrabEnd;

    public override void GrabBegin(OVRGrabber hand, Collider grabPoint)
    {
        OnGrabBegin.Invoke();
        base.GrabBegin(hand, grabPoint);
    }

    public override void GrabEnd(Vector3 linearVelocity, Vector3 angularVelocity)
    {
        base.GrabEnd(linearVelocity, angularVelocity);
        OnGrabEnd.Invoke();
    }
}

On the part side i now have this script attached to the grabbable go.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnapPart : MonoBehaviour
{
    private Transform snapPart;
    public float dist;

    private Vector3 initialLocalPosition;
    private Vector3 initialLocalRotation;
    private Transform initialParent;

    private OVRGrabbableExtended grabbable;

    private void Awake()
    {
        // get the grabbable component on this object
        grabbable = GetComponent<OVRGrabbableExtended>();
    }

    // Unity function called once when the game starts
    public void Start()
    {
        //Storing original position of the GO
        initialLocalPosition = transform.localPosition;
        initialLocalRotation = transform.localRotation.eulerAngles;
        initialParent = transform.parent;
    }

    public void Update()
    {
        dist = Vector3.Distance(snapPart.position, transform.position); // calc the distance
    }

    private void OnEnable()
    {
        // listen for grabs
        grabbable.OnGrabBegin.AddListener(OnGrabbed);
        grabbable.OnGrabEnd.AddListener(OnReleased);
    }

    private void OnDisable()
    {
        // stop listening for grabs
        grabbable.OnGrabBegin.RemoveListener(OnGrabbed);
        grabbable.OnGrabEnd.RemoveListener(OnReleased);
    }

    private void OnReleased()
    {

        if (dist < 0.5f)
        {
            // restore parent
            transform.SetParent(initialParent);
        }
   
    }

    private void OnGrabbed()
    {
        //
    }
}


Corysia
Level 5
I would recommend that you take out the calculation in Update().  You only need to know that number at one time -- when the object is released.  You're doing a lot of unnecessary math operations.  You don't need it as a member variable, either.  Make that call in your OnReleased() method.

Also, you're saving off the initialPosition.  But you're not restoring it if the distance is greater than your 0.5f threshhold.  You're only restoring the parent.

VJ76
Level 4
Ok, I now have the calculation in the OnRelease method, do i need to make a local variable (snapPart)? i'm not a pro at coding :# , so can you give me an idea of what it mustlook like? 

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class SnapPart : MonoBehaviour
{
    private Transform snapPart;

    private Vector3 initialLocalPosition;
    private Vector3 initialLocalRotation;
    private Transform initialParent;

    private OVRGrabbableExtended grabbable;

    private void Awake()
    {
        // get the grabbable component on this object
        grabbable = GetComponent<OVRGrabbableExtended>();
    }

    // Unity function called once when the game starts
    public void Start()
    {
        //Storing original position of the GO
        initialLocalPosition = transform.localPosition;
        initialLocalRotation = transform.localRotation.eulerAngles;
        initialParent = transform.parent;
    }

    private void OnEnable()
    {
        // listen for grabs
        grabbable.OnGrabBegin.AddListener(OnGrabbed);
        grabbable.OnGrabEnd.AddListener(OnReleased);
    }

    private void OnDisable()
    {
        // stop listening for grabs
        grabbable.OnGrabBegin.RemoveListener(OnGrabbed);
        grabbable.OnGrabEnd.RemoveListener(OnReleased);
    }

    private void OnReleased()
    {
        
        float dist = Vector3.Distance(snapPart.position, transform.position); // calc the distance

        if (dist < 0.5f)
        {
            // restore parent
            transform.SetParent(initialParent);
        }
        else if (dist > 0.5f)
        {

        }
    
    }

    private void OnGrabbed()
    {
        //
    }
}

Corysia
Level 5
I'll take a guess, but I can't really build and test this, tho.  These are the changes I'd make.
private Quaternion initialLocalRotation;

private void OnReleased()
{
float dist = Vector3.Distance(snapPart.position, transform.position);
if (dist < 0.5f)
{
transform.SetParent(initialParent); // restore parent
transform.SetPositionAndRotation(initialLocalPosition, initialLocalRotation);
}
}
And please,use the Code option under the Paragraph dropdown to make it easier to read.   ❤️

VJ76
Level 4
Unfortunately it is not working, i'd made multiple builds with different settings, like checking and un check kinematic and parent held object. But the cube is floating and rotating off continuously in the air, without any snapping.

You are certainly right, i was looking for that, but i could not find the code option.. now i can Thx!

Corysia
Level 5
It's an interesting problem and I may see if I can do it with my Example project.  It might be useful to restore the ball to the pedestal I have it sitting on if it rolls off the edge of the platform.  If I get to it this weekend, I'll let you know.

VJ76
Level 4
Maybe it has someting to do with physics. i dont know, it would be great if you can solve it! and thanks for helping so far! Wish you good luck, and have a nice weekend.