This site uses strictly necessary cookies. More Information

X- Home /

# 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.

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

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?

**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;
}
```

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.

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

**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.

### Your answer

### Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.

### Follow this Question

### Related Questions

When does Unity calculate vectors' magnitudes? 1 Answer

Top-Down Enemy Ai Animation 0 Answers

How do I do rb.AddForce but in 2D? 1 Answer