Search code examples
c#unity-game-enginecollisioneventtrigger

collide with the hand to trigger feedback


I am beginner in unity 3d programming, I have a project to integrate my feedback device with the virtual reality software (unity3d free version). I use leap motion to make my hand appear on the unity3d, and when the object (ball) touch the hand, it will trigger the feedback device to send the information.

I built the dll library for my feedback device, I have tried in c# and working.

I use onCollisionEnter to trigger the event. Here is my script:

using UnityEngine;
using System.Collections;
using System.Runtime.InteropServices; 

public class trigger : MonoBehaviour {  
[DllImport("tensLibs.dll", EntryPoint="tens_init")]
public static extern int tens_init ([MarshalAsAttribute(UnmanagedType.LPStr)]string port);
[DllImport("tensLibs.dll", EntryPoint = "tens_shutdown")]
public static extern void tens_shutdown();

[DllImport("tensLibs.dll", EntryPoint = "tens_settarget")]
public static extern int tens_settarget(byte id);

 [DllImport("tensLibs.dll", EntryPoint = "tens_write")]
 public static extern int tens_write([MarshalAsAttribute(UnmanagedType.LPStr)]string buf, int count);

[DllImport("tensLibs.dll", EntryPoint = "tens_enable")]
 public static extern int tens_enable(byte supply_on, byte bridge_on);

 [DllImport("tensLibs.dll", EntryPoint = "tens_power")]
public static extern int tens_power(byte power);

[DllImport("tensLibs.dll", EntryPoint = "tens_freq")]
public static extern int tens_freq(byte freq);

 [DllImport("tensLibs.dll", EntryPoint = "tens_chargerate")]
 public static extern int tens_chargerate(byte rate);

 [DllImport("tensLibs.dll", EntryPoint = "tens_writeconfig")]
 public static extern int tens_writeconfig();

 [DllImport("tensLibs.dll", EntryPoint = "tens_envs")]
public static extern int tens_envs(byte distance);

void OnCollisionEnter (Collision col) {

    if (col.gameObject.name == "bone1" || col.gameObject.name == "bone2" || col.gameObject.name == "bone3" || col.gameObject.name == "palm") {

         if (tens_init("COM8") == 1){
             Debug.Log ("Com 8 is initiated"); }
        else {
            Debug.Log ("Com 8 does not exists"); }


        tens_settarget(0); //send to device 0
        System.Threading.Thread.Sleep(10);
         tens_envs(150); //send value
         System.Threading.Thread.Sleep(10);
        tens_shutdown(); //close the com
     }

     }

The problem is, when I touch the ball, the event just showing me that the tens_init is working by appearing Com 8 is initiated but the rest of the program are not execute (I think because do not work).

Do I make mistake with the script?

thank you.


Solution

  • in unity you don't use the System.Threading.Thread.Sleep(...) syntax, you need to look into yield waitforseconds

    http://docs.unity3d.com/ScriptReference/WaitForSeconds.html