I'm developing a basic application to load and modify molecules and my main goal is to implement Kinect to do it. So far I've been able to do it with a mouse and I would like to do it with my right hand. After searching for a while I tried to define my cursor in a screen environment like if it would be done with a mouse (Input.mousePosition.x) substituting this for the hands coordinates (normalized for the screen dimensions). I can't make the cursor follow my hand, but I can make a game object follow any part of my body so my coordinates are being imported and when I debug my control variables they return a value contained in the screen dimensions. I guess my error is in OnGUI(). Can anybody help me.
Thanks in advance.
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class handPointer_01 : MonoBehaviour
{
public Texture2D cursorImage_00;
public Texture2D cursorImage_01;
public Texture2D cursorImage_02;
private Texture2D cursorImage;
private int cursorWidth = 16;
private int cursorHeight = 16;
private string defaultResource = "MousePointer";
private GameObject target;
public GameObject leftHandPos;
public GameObject rightHandPos;
public GameObject leftShoulderPos;
public GameObject rightShoulderPos;
public GameObject leftHipPos;
public GameObject rightHipPos;
private float RightHandX;
private float RightHandY;
private float xPrevious;
private float yPrevious;
private double MoveThreshold = 0.01;
void Start()
{
//Switch off default cursor
if(!cursorImage_00 || !cursorImage_01 || !cursorImage_02)
{
cursorImage = (Texture2D) Resources.Load(defaultResource);
Debug.Log(cursorImage);
}
else Cursor.visible = false;
//cursorImage = (Texture2D) Instantiate(cursorImage);
}
void Update()
{
if (rightShoulderPos.transform.position.z - rightHandPos.transform.position.z > 0.01)
{
float xScaled = Mathf.Abs((rightHandPos.transform.position.x - rightShoulderPos.transform.position.x) / ((rightShoulderPos.transform.position.x - leftShoulderPos.transform.position.x) * 2)) * Screen.width;
float yScaled = Mathf.Abs((rightHandPos.transform.position.y - rightHipPos.transform.position.y) / ((rightShoulderPos.transform.position.y - rightHipPos.transform.position.y) * 2)) * Screen.height;
// the hand has moved enough to update screen position (jitter control / smoothing)
if (Mathf.Abs(rightHandPos.transform.position.x - xPrevious) > MoveThreshold || Mathf.Abs(rightHandPos.transform.position.y - yPrevious) > MoveThreshold)
{
RightHandX = Mathf.Min(Mathf.Max(xScaled,Screen.width),0);
RightHandY = Mathf.Min(Mathf.Max(yScaled,Screen.height),0);
xPrevious = rightHandPos.transform.position.x;
yPrevious = rightHandPos.transform.position.y;
// reset the tracking timer
//trackingTimerCounter = 10;
}
}
// // Get the left mouse button
// if(Input.GetMouseButtonDown(0))
// {
// RaycastHit hitInfo;
// target = GetClickedObject (out hitInfo);
// if (target != null && target.gameObject.tag =="Draggable")
// {
// cursorImage = cursorImage_02;
// Debug.Log("Hit");
// }
// else
// {
// cursorImage = cursorImage_01;
// Debug.Log("Miss");
// }
// }
// // Disable movements on button release
// if (!Input.GetMouseButton(0))
// {
// cursorImage = cursorImage_00;
// }
cursorImage = cursorImage_00;
}
void OnGUI()
{
//GUI.DrawTexture(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, cursorWidth, cursorHeight), cursorImage);
GUI.DrawTexture(new Rect(RightHandX ,RightHandY , cursorWidth, cursorHeight), cursorImage);
}
GameObject GetClickedObject (out RaycastHit hit)
{
GameObject target = null;
Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);
if (Physics.Raycast (ray.origin, ray.direction * 10, out hit)) {
target = hit.collider.gameObject;
}
return target;
}
}
答案 0 :(得分:0)
我已经设法根据试错法尝试找到溶剂。 :) 删除了额外的代码(注释代码)以避免误解。 对谁感兴趣:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
public class HandModifier_03 : MonoBehaviour
{
//Cursor Variables
public Texture2D cursorImage_00;
private Texture2D r_cursorImage;
private int cursorWidth = 32;
private int cursorHeight = 32;
private string defaultResource = "MousePointer";
//Hand Variables
public Vector3 screenSpace;
private Vector3 last_right;
public Transform referenceCamera; //Camera that acts as a point of view to act on the object relative to.
// Kinect data receiver
public UDPReceive receiver;
private Vector3 headPos;
private Vector3 leftHandPos;
//private Vector3 leftWristPos;
//private Vector3 leftElbowPos;
private Vector3 leftShoulderPos;
private Vector3 rightHandPos;
//private Vector3 rightWristPos;
//private Vector3 rightElbowPos;
private Vector3 rightShoulderPos;
//private Vector3 leftThumbPos;
//private Vector3 rightThumbPos;
//private Vector3 leftHandTipPos;
//private Vector3 rightHandTipPos;
private Vector3 leftHipPos;
private Vector3 rightHipPos;
private string lHandState;
private string rHandState;
//Cursor auxiliar variables
private float xScaled;
private float yScaled;
private Vector2 rHandScreen;
void Start()
{
r_cursorImage = cursorImage_00;
}
// Update is called once per frame
void Update ()
{
//Coordinate Update
headPos = receiver.headPos;
leftHandPos = receiver.leftHandPos;
//leftWristPos = receiver.leftWristPos;
// leftElbowPos = receiver.leftElbowPos;
leftShoulderPos = receiver.leftShoulderPos;
rightHandPos = receiver.rightHandPos;
// rightWristPos = receiver.rightWristPos;
// rightElbowPos = receiver.rightElbowPos;
rightShoulderPos = receiver.rightShoulderPos;
// leftThumbPos = receiver.leftThumbPos;
// rightThumbPos = receiver.rightThumbPos;
// leftHandTipPos = receiver.leftHandTipPos;
// rightHandTipPos = receiver.rightHandTipPos;
lHandState = receiver.lHandState;
rHandState = receiver.rHandState;
leftHipPos = receiver.leftHipPos;
rightHipPos = receiver.rightHipPos;
//Right Hand Screen Position
xScaled = Mathf.Abs ((rightHandPos.x - rightShoulderPos.x)) / Mathf.Abs ((rightShoulderPos.x - leftShoulderPos.x) * 1.75f) * Screen.width;
yScaled = Mathf.Abs ((rightHandPos.y - rightHipPos.y)) / Mathf.Abs ((rightShoulderPos.y - rightHipPos.y) * 2) * Screen.height;
xScaled = Mathf.Max (Mathf.Min (xScaled, Screen.width * 0.98f), 0.02f);
yScaled = Screen.height - Mathf.Max (Mathf.Min (yScaled, Screen.height * 0.98f), 0.02f); // Subtracting Screen Height required
Vector2 rHandScreen = new Vector2 (xScaled, yScaled);
// Vector2 rHandScreen = HandOnScreenPosition (out rightHandPos, leftShoulderPos, rightShoulderPos, rightHipPos);
// UnityEngine.Debug.Log(_mode);
}
void OnGUI()
{
//GUI.DrawTexture(new Rect(Input.mousePosition.x, Screen.height - Input.mousePosition.y, cursorWidth, cursorHeight), cursorImage);
GUI.DrawTexture(new Rect(xScaled ,yScaled , cursorWidth, cursorHeight), r_cursorImage);
}
}