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 Gidaio · Sep 15, 2013 at 10:31 PM · vector3joystickinput.getaxis

Capture Joystick Direction Accurately

I'm trying to use the joystick (and keyboard keys) to select a direction without actually travelling in said direction. For example, I want to point an arrow in a direction using the joystick. However, when I release the joystick, I want the arrow to remain in that direction without changing. Here's the code I've come up with so far (in JavaScript):

 var dir : Vector3;
 var randomObject : Transform;
 
 function Update()
 {
     var horiz = Input.GetAxis("Horiz");
     var vert = Input.GetAxis("Vert");
     // Check that they're not BOTH zero - otherwise
     // dir would reset because the joystick is neutral.
     if(horiz != 0 || vert != 0))
     {
         dir.x = horiz;
         dir.z = vert;
         dir.Normalize();
     }
     randomObject.position = dir;
 }

This works when going up, right, down, or left. It also works when I press a diagonal direction; however, when I release the keys or joystick, it's pretty much random whether it stays that direction or heads off to the left or right. I know it's just a matter of processing the input correctly, but I'm at a loss as to how to do this. Any ideas?

I don't care what programming language an answer is in, and I'll try to keep working this out myself.

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 Gidaio · Sep 16, 2013 at 12:12 AM

So, I ended up solving this by adding a 'timing' dead zone - a small grace period for the user if they're transitioning AWAY from a diagonal value. I have yet to fully test it with a joystick, but it works with WASD quite nicely.

 var timeDeadZone = 0.0;
 var dir : Vector3;

 function Update()
 {
     var moveHoriz = Input.GetAxis("MoveHoriz");
     var moveVert = Input.GetAxis("MoveVert");
     // If one of them's not zero, set moveDir
     if(moveHoriz != 0 || moveVert != 0)
     {
         // If we're coming from a diagonal direction, wait for a bit
         if(Mathf.Abs(moveDir.x) > 0 && Mathf.Abs(moveDir.z) > 0 && currTimeDead <= 0)
             currTimeDead = 0.1;

         if(currTimeDead > 0)
             currTimeDead -= Time.deltaTime;

         // If we're done waiting, set the direction
         if(currTimeDead <= 0)
         {
             moveDir.x = moveHoriz;
             moveDir.z = moveVert;
             moveDir.Normalize();
         }
     }
 }

Thanks to those who answered for taking time to do so, though!

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 RIPANN · Nov 27, 2019 at 09:26 AM 0
Share

This "ti$$anonymous$$g" dead zone is a good trick for improving accuracy.

avatar image
0

Answer by Bunny83 · Sep 15, 2013 at 11:16 PM

You simply need a dead zone. At the moment you only have a "dead point" which is 0,0. If you release the stick one axis will probably reach 0 before the other or bounce around 0 for a short moment. This will result in a more or less random last result when both axis reach 0. The solution is to create a dead zone:

 var dir : Vector3;
 var randomObject : Transform;
 var deadZone = 0.2;
 
 function Update()
 {
     var horiz = Input.GetAxis("Horiz");
     var vert = Input.GetAxis("Vert");
     var tmp = Vector3(horiz, 0, vert);
     if(tmp.sqrMagnitude > deadZone)
     {
         dir = tmp.normalized
     }
     randomObject.position = dir;
 }
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 Gidaio · Sep 15, 2013 at 11:29 PM 0
Share

I thought of this, too - just make a bigger dead zone! Although I don't know why I'd need code when I can set the dead zone in the Input $$anonymous$$anager. I did try this anyway, but to no avail - I ended up solving it with a 'ti$$anonymous$$g' dead zone, if you will.

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

18 People are following this question.

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

Related Questions

Getting Input.GetAxis to Execute Once 3 Answers

Rotating object on one axis using Input.GetAxis 2 Answers

Moving a cube on the X-axis only moves by 1 unit? 1 Answer

Rotate a GameObject in the Vector3 direction? 1 Answer

Get the position of the first finger that touched the collider? 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