Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 NickJaGr01 · Nov 22, 2016 at 09:05 AM · c#movementbug-perhaps

Input.GetAxis not returning to zero after key is released

So I'm using Input.GetAxis to retrieve user input for driving a car. I'm am particularly having issues with my vertical axis. When I debug, my horizontal axis show the same problem, however for some reason it does not show any symptoms in-game. By the way, I am using wasd controls.

Anyway, when I press and hold 'w', the car accelerates forwards, and also accelerates backwards if I press and hold 's'. This is as expected. Now lets say I'm going forward, I then let go of 'w' and the car continues to accelerate. The only way I can get it to stop is if I then hold 's' to force the car in the opposite direction. But then even if I let go of 's' the car continues to decelerate before continuing to accelerate backwards. This just goes on and on in an endless cycle.

I will use vertical as a substitution for Input.GetAxis("Vertical"), just for the sake of readability.

I did some debugging and found that vertical does not return to zero when I release 'w' or 's'. When I press 'w' vertical rises to 1 or a value very close to 1, just as expected. However, when I release 'w', vertical does not return to 0, rather it lowers to a number close to zero, usually between 0.1 and 0.2. The only way to make it lower is to press the opposite direction, which makes vertical -1 before then becoming a value between -.2 and -.1 when i release the button.

Now the max torque on my car is 28552.05, so even if vertical was as low as 0.01, I would still be setting the torque of the car's wheels to 285.52 every physics update.

Another interesting thing I found was that vertical seems to be consciously avoiding being 0. Now obviously that is not possible, but here's what I mean by that. I attempted to fix the problem by modifying my code to compensate and set vertical to zero if Input.GetAxis("Vertical") returned anything with an absolute value less than a constant value threshold. I initially made threshold equal to 2. Then I increased it to 2.5. Each time I increased threshold my debugging results changed. I found that the higher the value of threshold, the higher the lowest value of vertical. What I mean by that is whenever I increased the value of threshold, Input.GetAxis("Vertical") still lowered and approached 0 when I released 'w', but it lowered to a higher value than before and vertical never dropped below threshold. I am confused as to how this specific phenomena is possible, and I am particularly interested in how it occurs.

My expected result from releasing 'w' when the car is moving forwards is for it to stop accelerating and begin to coast and lose speed. I am unsure of why I am not getting this result.

Here is my code. I will remove anything related to the horizontal axis since it should have the same solution as the vertical axis.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class DriveController : MonoBehaviour {
     public List<AxleInfo> axleInfos; // the information about each individual axle
     public float maxMotorTorque; // maximum torque the motor can apply to wheel
     public float speedThreshold;
     public int stepsBelowThreshold;
     public int stepsAboveThreshold;
 
     void Start () {
         foreach (AxleInfo axleInfo in axleInfos) {
             axleInfo.leftWheel.ConfigureVehicleSubsteps (speedThreshold, stepsBelowThreshold, stepsAboveThreshold);
             axleInfo.rightWheel.ConfigureVehicleSubsteps (speedThreshold, stepsBelowThreshold, stepsAboveThreshold);
         }
     }
     
     void FixedUpdate () {
         float vertical = Input.GetAxis("Vertical");
         float motor = maxMotorTorque * vertical;
 
         foreach (AxleInfo axleInfo in axleInfos) {
             if (axleInfo.motor) {
                 axleInfo.leftWheel.motorTorque = motor;
                 axleInfo.rightWheel.motorTorque = motor;
             }
         }
     }
 }
 
 [System.Serializable]
 public class AxleInfo {
     public WheelCollider leftWheel;
     public WheelCollider rightWheel;
     public bool motor; // is this wheel attached to motor?
 }
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

0 Replies

· Add your reply
  • Sort: 

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Making a bubble level (not a game but work tool) 1 Answer

WSAD controls work fine for W and S, but randomly get reversed for A and D 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Problems with walking animation 0 Answers


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