Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 biebuster · Oct 03, 2015 at 08:13 PM · rotationjoystickeulerangles360-degreesanalog

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 -

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

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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.

Comment
Add comment · Show 1 · 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 SpaceManDan · Mar 13, 2021 at 05:20 AM 0
Share

Nice, works great! Thanks.

avatar image
0

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.

Comment
Add comment · Show 1 · 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 Trlgger · Oct 03, 2015 at 10:05 PM 0
Share

I realize now that this might not be so helpful for your rotation question. This is answered in a previous post here

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to smoothly rotate object in direction of analog joystick? 1 Answer

eulerAngles.y always returns 179.9999 1 Answer

Rotate Locally Player Towards Joystick/Axis Input 1 Answer

Best way to rotate a door 1 Answer

Keep track of rotations beyond 360 degrees, and under 0 degrees 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