• 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 Rhombuster · Jan 07, 2015 at 01:23 AM · vector2magnitude

Xbox stick vector magnitude confusion

I'm a little bit confused by the way the xbox stick is calculating its vector measurements when pushed in certain directions.

My problem is that I'd like to have 8 different directions to mimic fighting game style input. So I figured I'd use something to create movements that felt less restrictive. So I thought something like this would work:

 if(someVector.x > 0.3f && someVector.x <= 1.0f && someVector.y > 0.3f && someVector.y <= 1.0f)
     upRightDiagonal = true;
 

The idea is to give a larger input zone for the sticks so that it isn't a perfect 45 degree angle.

The weird part is that when using the keyboard for input, pushing forward and up gives me the 2d vector values (1,1). When using an xbox controller stick and going near what I would expect would be the equivalent of (1,1) gives results closer to (0.8,0,8) or something like that. Is it being normalized? I can never get the sticks to reach (1,1). Why does it do this? Is every controller like this or is mine just borked? I don't want to create these restrictions and then realize my controller is just messed up.

I forgot to mention that the controller will properly do (-1,0), (1,0), (0,1) and (0,-1) to its appropriate left, right, up and down movements. It's only when trying to get angles angles between those. I also tried to use Vector2.Angle to calculate but it didn't seem to reach the angles I wanted. Sorry for the long post, thanks to anyone who reads or responds.

Comment
Add comment · Show 2
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 HYPERSAVV · Jan 07, 2015 at 07:45 AM 0
Share

Pushing the gamepad stick around the edge will give you points along the outside of a circle. (1, 1) would lie outside of that. $$anonymous$$eyboard input is digital (compared to the analog gamepad stick) and is either 0 (not pressed) or 1 (pressed).

avatar image Industrion · Apr 02, 2015 at 06:06 PM 0
Share

I found that, using the Xbox 360 controller driver on $$anonymous$$ac OSX, the values yield a very-rounded rectangle shape - close to a circle, but definitely not one. Can anyone shed any light on this?

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by DwaynePritchett · Jan 07, 2015 at 11:43 AM

I didn't actually test it. But you should be able to use this and then just check in between what ranges you want.

     float GetInputAngle(float xInput, float yInput){
         //Get the angle ignoring quadrant. Call it rawAngle.
         float absX = Mathf.Abs(xInput);
         float absY = Mathf.Abs(yInput);
 
         if(xInput > 0){
             float rawAngle = Mathf.Rad2Deg*Mathf.Atan(absY/absX);
             if(yInput > 0){
                 return rawAngle;
             }
             if(yInput < 0){
                 return 360 - rawAngle;
             }
             if(yInput == 0){
                 return 0;
             }
         }
         if(xInput < 0){
             float rawAngle = Mathf.Rad2Deg*Mathf.Atan(absY/absX);
             if(yInput > 0){
                 return 180 - rawAngle;
             }
             if(yInput < 0){
                 return 180 + rawAngle;
             }
             if(yInput == 0){
                 return 180;
             }
         }
         if(xInput == 0){
             if(yInput > 0){
                 return 90;
             }
             if(yInput < 0){
                 return 270;
             }
             if(yInput == 0){
                 //This means no input.
                 return -1;
             }
         }
         return -1;
     }



alt text


anglediagram.png (9.4 kB)
Comment
Add comment · Show 2 · 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 Rhombuster · Jan 07, 2015 at 06:46 PM 0
Share

Thanks guys, I was laying in bed last night and realized this was just the unit circle from my calc class. I hate how it seems like you always figure something out when you're doing nothing even though you spent a couple hours lost and frustrated. I haven't tried your code but that is definitely what I wanted. Thanks so much for the help guys.

avatar image DwaynePritchett · Jan 07, 2015 at 09:28 PM 0
Share

Check as answered then please. And, you're welcome.

avatar image
0

Answer by DwaynePritchett · Jan 07, 2015 at 12:28 PM

I don't know if I understand the issue...but...

The thing is, at best you can only give each of the inputs a 45 degree threshold. Because, 360/8 is 45.

I think you aren't considering the fact that the space around the Xbox's Analog stick isn't a square, but rather a circle. If it was normalized it would be (0.707,0.707) approximately. Because, a 45 degree angle is (root2/2). Since, you're getting some 0.8 values, the circular shape around the stick must not be a perfect "Unit Circle".

I would make a Quaternion and return the 2D Rotation Equivalent of my input as some degrees. Then use that to determine the direction you are pushing. Where,(-22.5,22.5) is Right.(22.5,67.5) is UpRight etc.

I feel like Unity has this built in, but I don't know what off the top of my head.

Hope this is somewhat helpful at least.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

When does Unity calculate vectors' magnitudes? 1 Answer

Top-Down Enemy Ai Animation 0 Answers

Converting direction to Vector2 2 Answers

Is there a more efficient way to pass two integers via "SendMessage" than using a Vector2 Struct? 1 Answer

Rotating A Vector2 Input Into A Vector3 1 Answer


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