- Home /
Scaling issue using a GUITexture on a sprite object in unity
I'm trying to make a Kinect game using MS-SDK wrapper from the Asset store in Unity, In the kinect manager script the Hand Cursor object is where you can set the gameObject you want to act as a cursor.
The example provided by MSSDK gesture demo uses a GUITexture as the Hand cursor, however to be able to detect collisions, the box collider component attached to the hand cursor needs a transform scale value set to atleast 0.1, but setting the scale value causes the Texture to appear very large.
I would prefer to use sprites over GUITexture, since most of the gameObjects will be sprites but the movement as a sprite object is very limited, the object doesnt really move as much as the hand.
The same issue occurs, if I create a sprite and add an GUITexture component, or if I just use a sprite then it doesn't move with my hand properly (very less translation).
In short, I want to be able to use a hand cursor and detect collision with other sprites in the game.
To understand a bit about whats going on with the code, here's a snippet
RightHandCursor Gesture detection:
// check for RightHandCursor
case Gestures.RightHandCursor:
switch(gestureData.state)
{
case 0: // gesture detection - phase 1 (perpetual)
if(jointsTracked[rightHandIndex] && jointsTracked[rightHipIndex] &&
(jointsPos[rightHandIndex].y - jointsPos[rightHipIndex].y) > -0.1f)
{
gestureData.joint = rightHandIndex;
gestureData.timestamp = timestamp;
//gestureData.jointPos = jointsPos[rightHandIndex];
SetScreenPos(userId, ref gestureData, ref jointsPos, ref jointsTracked);
gestureData.progress = 0.7f;
}
else
{
// cancel the gesture
//SetGestureCancelled(ref gestureData);
gestureData.progress = 0f;
}
break;
}
break;
The SetScreenPos function:
private static void SetScreenPos(uint userId, ref GestureData gestureData, ref Vector3[] jointsPos, ref bool[] jointsTracked)
{
Vector3 handPos = jointsPos[rightHandIndex];
bool calculateCoords = false;
if(gestureData.joint == rightHandIndex)
{
if(jointsTracked[rightHandIndex] /**&& jointsTracked[rightElbowIndex] && jointsTracked[rightShoulderIndex]*/)
{
calculateCoords = true;
}
}
else if(gestureData.joint == leftHandIndex)
{
if(jointsTracked[leftHandIndex] /**&& jointsTracked[leftElbowIndex] && jointsTracked[leftShoulderIndex]*/)
{
handPos = jointsPos[leftHandIndex];
calculateCoords = true;
}
}
if(calculateCoords)
{
if(jointsTracked[hipCenterIndex] && jointsTracked[shoulderCenterIndex] &&
jointsTracked[leftShoulderIndex] && jointsTracked[rightShoulderIndex])
{
Vector3 neckToHips = jointsPos[shoulderCenterIndex] - jointsPos[hipCenterIndex];
Vector3 rightToLeft = jointsPos[rightShoulderIndex] - jointsPos[leftShoulderIndex];
gestureData.tagVector2.x = rightToLeft.x; // * 1.2f;
gestureData.tagVector2.y = neckToHips.y; // * 1.2f;
if(gestureData.joint == rightHandIndex)
{
gestureData.tagVector.x = jointsPos[rightShoulderIndex].x - gestureData.tagVector2.x / 2;
gestureData.tagVector.y = jointsPos[hipCenterIndex].y;
}
else
{
gestureData.tagVector.x = jointsPos[leftShoulderIndex].x - gestureData.tagVector2.x / 2;
gestureData.tagVector.y = jointsPos[hipCenterIndex].y;
}
}
if(gestureData.tagVector2.x != 0 && gestureData.tagVector2.y != 0)
{
Vector3 relHandPos = handPos - gestureData.tagVector;
gestureData.screenPos.x = Mathf.Clamp01(relHandPos.x / gestureData.tagVector2.x);
gestureData.screenPos.y = Mathf.Clamp01(relHandPos.y / gestureData.tagVector2.y);
}
}
}
Answer by robertbu · Oct 11, 2014 at 02:42 PM
GUITextures live in Viewport space. Viewport space starts at (0,0) in the lower left and go to (1,1) in upper right. So to use a sprite, you need to modify the conversion to use world coordinates. In the cursor code you will likely see a ScreenToViewportPoint() call. This call needs to be ScreenToWorldPoint().
Currently, the project works perfectly if I use kinect with the GUITexture as a RightHandCursor object and according to you the GUITextures live in the viewport space. So it means I should also try to convert the sprite into the viewport space coordinates as well right?
I have inserted a bit of snippet about the gesture and the screen position function, so that it might help get an idea about whats going on, I feel there's something i need to change in the screenpos function to help me move the sprite object around just like the GUITexture
You cannot use Viewport coordinates directly with a sprite. You can convert between the two using Camera.ViewportToWorldPoint(). Note you may have to pay attention to the 'z' coordinate passed depending on your situation. It might be easier to hide the current hand cursor and write some code that has the sprite tracking the current hand cursor rather than rewriting this code. It is a hackish solution, but it might be simpler and faster.
Your answer
Follow this Question
Related Questions
How to rebuild Atlas manually? 1 Answer
If/Then Conditional Ignoring Usual Logic C# 0 Answers
Why does this sprite's normal map only light correctly from one direction? 2 Answers
Why some sprites in spritesheets are not visible in sprites but works fine in UI images? 0 Answers
How do i remove the parent spritesheet name in all sliced sprites? 0 Answers