1

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.

Maaike
  • 11
  • 2
  • show some working please – Saad Anees Apr 25 '19 at 09:50
  • I don't really know what the show. I have a scene with the leap motion hands, a cube, and a ground so the blocks don't fall down. The cube has a script with a OnCollisionEnter function that prints to the console if a collision is detected but that only prints something if the object falls to the ground, not when the hand touches the object. And the cube has the InteractionBehaviour script. Other than that, there is not really anything. What do you want me to show? – Maaike Apr 25 '19 at 09:59
  • @remy_rm Thanks! I edited the post. Hope this makes it more clear. – Maaike Apr 25 '19 at 13:50
  • Do your "hands" object have a collider attached to them? And if they do, do they have "Is Trigger" checked? Collisions can only happen between two objects that have colliders on them, and set "Is Trigger" to false. If the hands have "Is Trigger" checked you need to use `OnTriggerEnter` instead – Remy Apr 25 '19 at 14:00
  • @remy_rm That was the problem thank you! I now managed to get the object to stay in the middle of the index finger and thumb position once they have made contact with the object. – Maaike Apr 25 '19 at 15:58
  • Glad that it helped. For future reference I've worked my comment out into a full answer. – Remy Apr 25 '19 at 17:00

1 Answers1

1

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

Remy
  • 4,843
  • 5
  • 30
  • 60
  • Yes! And then to make it stick you can just parent it to the collider it collided with, and make it ignore future triggers to make it stay there. – HoloLady Apr 26 '19 at 07:53