- Home /
need the game to detect if the analog stick is at the opposite angle
So the the charachter in our scene has two arms. The angle of both of these arms, is simply just the angle of the left analog stick. Now i want the game to detect if the player suddenly, changes the rotation of these arms to the opposite like this: http://imgur.com/dCFDbbT http://imgur.com/K6QOHPI Now obiously it doesn't have to be EXACTLY the opposite. Just something that is in reach of the opposite, wich i do know how to do. But there are alot of problems: the 360 value problem: when something reaches 360 eulerangles, it will go back to 0 eulerangles in the game. now let's say i was to do this. (imagine the current eulerangle of our arm is 175)
maxRange = transform.eulerAngles.z + 180 + 15
plussing it by 180 would give us 355. that plus 15 is 370 and even if i could get the value down to what would in this instance be 10, it would still be a problem because at some point i will have to say:
if (oldRotation < maxRange) {
if (oldRotation > minRange) {
//do stuff here
the maxRange would then be 10. And that woulnd't be very good. So you probaly understand what i'm getting at here.
another problem is how im supposed to get the game to know that, this new angle is very different to the current one. when that current angle could change any frame. would i just keep a "oldRotation" that changes every second frame? Kind regards -
Answer by Eno-Khaon · Oct 04, 2015 at 05:44 AM
If you keep track of the current direction the analog stick is held in a fairly generalized manner, you can determine whether a large change was recently made.
For example, consider the idea that the analog stick's direction can go no further than 1.
// C#
// ...
Vector2 lastDirection;
public float oppositeThreshhold = -0.9f; // How precisely does the analog stick have to be pressed in the opposite direction it was previously?
// ...
void Update()
{
// ...
Vector2 inputDirection = new Vector2(Input.GetAxisRaw("Horizontal"), Input.GetAxisRaw("Vertical")); // GetAxisRaw for more precise reading of analog stick direction
// Normalize input only if it exceeds the maximum for either axis alone.
if(inputDirection.sqrMagnitude > 1)
{
inputDirection = inputDirection.normalized;
}
if(inputDirection.sqrMagnitude > 0.95f)
{
if(Vector2.DoT(inputDirection, lastDirection) < oppositeThreshhold)
{
// Opposite Direction
}
lastDirection = inputDirection;
}
Throw in some additional variables, like a timer to limit the speed at which the player can change the orientation and that should cover a lot of the major work.
By requiring the analog stick be held mostly in a direction (again, easy to replace 0.95f with a variable), you make certain that there won't be erroneous detection of input as the analog stick returns to the center before being pressed in a new direction.
Additionally, keeping track of more than just a single "lastDirection" can help to add a bit more diversity, in case they press close to the opposite direction, but then have it in the right spot a brief moment later (and still within a reasonably short amount of time).
The problem most easily encountered with this sort of detection is that the better the game is performing, the easier it is to miss being in the right place at the right time, so keeping track of additional data can prove very resourceful.
Answer by Trlgger · Oct 03, 2015 at 09:58 PM
Unity has a built in Input function for checking analog tilt. 0 is middle, 1 is right, -1 is left and everything else is the distance. You can use these values to achieve what your going for.
Example:
void Update(){
if(Input.GetAxis("Horizontal") > 0){
//Joystick Right
}
if(Input.GetAxis("Horizontal") < 0){
//Joystick Left
}
}
I hope this is what you need.
I realize now that this might not be so helpful for your rotation question. This is answered in a previous post here