I'm working on a project with the Leap Motion Controller and Unity. I have a player (a mouse) that is walking randomly and I am trying to grab him with my hands with the Leap Motion Controller. However when my hands are detected, the player flies away and does not return.
This is my script:
using System;
using System.Collections;
using System.Collections.Generic;
using Leap.Unity.Interaction;
using UnityEngine;
using Random = System.Random;
public class MouseHandler: MonoBehaviour
{
private bool enter = false;
private Vector3 target;
private Vector3 mouseposition;
[SerializeField] private float stopdis;
private InteractionBehaviour interactionbehaviour;
[SerializeField] private InteractionHandler interactionhandler;
void Start()
{
target = transform.position;
interactionbehaviour = GetComponent<InteractionBehaviour>();
interactionbehaviour.enabled = false;
mouseposition = transform.position;
}
void Update()
{
if (Vector3.Distance(mouseposition, target) < stopdis)
{
float x = UnityEngine.Random.Range(0f, -8f);
float z = UnityEngine.Random.Range(4f, -4f);
target.x = x;
target.z = z;
//print(target);
}
}
private void FixedUpdate()
{
if (Vector3.Distance(mouseposition, interactionhandler.rightpalmposition) < 0.1f)
{
print("hi");
interactionbehaviour.enabled = true;
}
else
{
mouseposition = transform.position;
mouseposition = Vector3.Lerp(mouseposition, target, 0.05f);
transform.position = mouseposition;
}
}
I think it has something to do with the Interaction Behaviour of the Leap Motion Controller, but I am not sure..