Maaike
Maaike

Reputation: 11

How can I let an object stick to the hand instead of dropping it

For a project I want to create a VR environment using Leap Motion in which objects can be picked up. So far picking up an object is working (using the InteractionEngine module from Leap and the InteractionBehaviour script), but the objects are dropped a lot because the Leap Motion apparently thinks that the hand is letting go of the object. For the purpose that I want to use it for, it would be sufficient to stick the object to the hand once it's been grasped, sort of like sticky fingers. Can anyone help me with this? I'm very new to Leap Motion and have no idea where to start.

I've tried to use OnCollisionEnter (I have no idea whether this is a good place to start tough) but it doesn't seem to register a collision when the hand touches the object. It only sees a collision when the object falls to the ground.

Edit: More info about the environment + what I've tried with OnCollisionEnter:

This is the environment that I have right now

These are the important components of the object. (ResetFood is a small script I wrote to reset the cube to its original position and doesn't really matter for this).

This is the script in which I've tried to use OnCollisionEnter.

using System.Collections.Generic;
using UnityEngine;

public class StickToHand : MonoBehaviour
{

    private void OnCollisionEnter(Collision collision) {
        Debug.Log("Collision detected");
    }
}

Which only prints Collision detected when the object falls on the ground.

Upvotes: 1

Views: 444

Answers (1)

Remy
Remy

Reputation: 5163

The reason Debug.Log("Collision detected"); is not being hit is most likely because you're using a (or more) collider that has the Is Trigger option checked. enter image description here

This will turn the collider into a Trigger. Which will make it ignore the actual collision, and instead only checks if the collider has entered the space of another collider.

As per the Unity docs on triggers:

A collider configured as a Trigger (using the Is Trigger property) does not behave as a solid object and will simply allow other colliders to pass through. When a collider enters its space, a trigger will call the OnTriggerEnter function on the trigger object’s scripts.

This in turns means that if either of the colliders is a trigger instead private void OnCollisionEnter(Collision collision) will not be called, as there is no collision.

Instead OnTriggerEnter(Collider collider) is called.

The reason Collision detected is printed when the cube falls is because both your cube and ground are not triggers, but solid colliders. Leaving it most likely that your hands are set up to have triggers.

If you use

private void OnTriggerEnter(Collider collider)
{
    Debug.Log("Trigger detected");
}

Instead it should log Trigger detected in the console when you touch the object. Inside this method you'll then also be able to manipulate the object you're touching using collider.gameObject

Upvotes: 1

Related Questions