Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by sweptica · Oct 11, 2014 at 02:24 PM · spriteskinect

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);
     
             }
         }
     }


Scaling issue

1.jpg (21.2 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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().

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image sweptica · Oct 12, 2014 at 02:45 PM 0
Share

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?

avatar image sweptica · Oct 12, 2014 at 03:00 PM 0
Share

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

avatar image robertbu · Oct 12, 2014 at 03:38 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges