Snapping using kinect v2 sdk
Hello guys, i got some kind of question maybe it is a lil complex. i am using kinect v2 sdk and drag and drop scripts from it. i tries to add a snapping script using colliders enter and mouse release method. my script works fine with mouse dap and drop but when it comes to control via kinect it looses snapping effect. so my question is how to get it configured to work with kinect libraries?
Answer by ashomran · Oct 28, 2018 at 12:40 PM
this is my snapping script.
using UnityEngine;
using System.Collections;
public class Snap : MonoBehaviour {
public string partnerTag;
public float closeVPDist = 0.05f;
public float farVPDist = 1;
public float moveSpeed = 40.0f;
public float rotateSpeed = 90.0f;
private Vector3 screenPoint;
private Vector3 offset;
private bool isSnaped;
Color color = new Color(1, 0, 0);
float dist = Mathf.Infinity;
Color normalColor;
GameObject partnerGO;
// Use this for initialization
void Start () {
normalColor = GetComponent<Renderer>().material.color;
partnerGO = GameObject.FindGameObjectWithTag(partnerTag);
}
void OnMouseDown()
{
screenPoint = Camera.main.WorldToScreenPoint(transform.position);
offset = transform.position - Camera.main.ScreenToWorldPoint(new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
Cursor.visible = false;
}
void OnMouseDrag()
{
//transform.SetParent(null);
Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);
Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
transform.position = curPosition;
Vector3 partnerPos = Camera.main.WorldToViewportPoint(partnerGO.transform.position);
Vector3 myPos = Camera.main.WorldToViewportPoint(transform.position);
dist = Vector2.Distance(partnerPos, myPos);
GetComponent<Renderer>().material.color = (dist < closeVPDist) ? color : normalColor;
}
void OnMouseUp()
{
Cursor.visible = true;
if (dist < closeVPDist)
{
transform.SetParent(partnerGO.transform);
StartCoroutine(InstallPart());
isSnaped = true;
}
if( dist > farVPDist)
{
// transform.SetParent(null);
}
}
IEnumerator InstallPart()
{
while (transform.localPosition != Vector3.right || transform.localRotation != Quaternion.identity)
{
transform.localPosition = Vector3.MoveTowards(transform.localPosition, Vector3.right, Time.deltaTime * moveSpeed);
transform.localRotation = Quaternion.RotateTowards(transform.localRotation, Quaternion.identity, Time.deltaTime * rotateSpeed);
yield return new WaitForEndOfFrame();
}
}
}
it works fine with mouse controling.
i need to change (On$$anonymous$$ouseUp, On$$anonymous$$ouseDown, On$$anonymous$$ouseDrag) to be like kinect call it HandRelease,Grip and none
Your answer
![](https://koobas.hobune.stream/wayback/20220612181112im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Moving an object a fixed amount on its local axis 1 Answer
How would I Consistantly 'Snap' a UI Element to the Edge of a Camera? 1 Answer
snap object to another object 2 Answers
Swipe to rotate an object and when it reach 30° snap it to 90° 0 Answers
Need help with snapping Prefab assets to grid in game 2 Answers